-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathArbitrarySendErc20.sol
More file actions
1032 lines (883 loc) · 37.4 KB
/
Copy pathArbitrarySendErc20.sol
File metadata and controls
1032 lines (883 loc) · 37.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//@compile-flags: --only-lint arbitrary-send-erc20
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
interface IERC20 {
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
}
interface IERC721 {
// Same name as ERC20.transferFrom but a different ABI; should NOT match.
function transferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId) external;
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
}
interface IERC3156FlashBorrower {
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external returns (bytes32);
}
// Same method name as EIP-3156, different signature.
interface IFakeFlashBorrower {
function onFlashLoan(bytes calldata data) external;
}
library SafeERC20 {
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
// Library passthrough — `from` is forwarded by the caller; the lint fires at the
// user's call site (see `badLibrary`), not here.
require(token.transferFrom(from, to, value), "SafeERC20: transferFrom failed");
}
function safePermit(
IERC20 token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
token.permit(owner, spender, value, deadline, v, r, s);
}
}
contract ArbitrarySendErc20 {
using SafeERC20 for IERC20;
IERC20 public token;
IERC20 public other;
IERC721 public nft;
address public owner;
address public immutable trustedOwner;
constructor(address _trustedOwner) {
trustedOwner = _trustedOwner;
}
function _msgSender() internal view returns (address) {
return msg.sender;
}
// Verifies depth-bounded helper recognition.
function _origin() internal view returns (address) {
return _msgSender();
}
// -- POSITIVE CASES (should warn) --
function badPlain(address from, address to, uint256 a) public {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badSafeMember(address from, address to, uint256 a) public {
token.safeTransferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badLibrary(address from, address to, uint256 a) public {
SafeERC20.safeTransferFrom(token, from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Reassignment kills earlier safety.
function badReassign(address from, address to, uint256 a) public {
address x = msg.sender;
x = from;
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Ternary with one unsafe branch.
function badTernary(bool flag, address from, address to, uint256 a) public {
address x = flag ? msg.sender : from;
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Tuple destructuring picks the unsafe slot.
function badTuple(address from, address to, uint256 a) public {
(, address x) = (msg.sender, from);
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Index reads are opaque.
function badIndex(address[] calldata senders, address to, uint256 a) public {
token.transferFrom(senders[0], to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badPermitWrongToken(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
other.permit(from, address(this), a, deadline, v, r, s);
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badPermitWrongSpender(
address from,
address spender,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(from, spender, a, deadline, v, r, s);
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badPermitAfter(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
token.permit(from, address(this), a, deadline, v, r, s);
}
// Permit on a side branch must not suppress the fall-through path.
function badPermitInOtherBranch(
bool flag,
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
if (flag) {
token.permit(from, address(this), a, deadline, v, r, s);
}
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Token reassigned after permit invalidates the record.
function badPermitTokenReassigned(
IERC20 t,
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
t.permit(from, address(this), a, deadline, v, r, s);
t = other;
t.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Owner reassigned after permit invalidates the record.
function badPermitOwnerReassigned(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
address x = from;
token.permit(x, address(this), a, deadline, v, r, s);
x = to;
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Disjunction does not establish equality.
function badDisjunction(address from, address to, uint256 a) public {
require(from == msg.sender || to == msg.sender, "weak");
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Guard scoped to one branch must not leak.
function badGuardScoped(bool flag, address from, address to, uint256 a) public {
if (flag) {
require(from == msg.sender, "ok in this branch");
}
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badInterfaceCast(address rawToken, address from, address to, uint256 a) public {
IERC20(rawToken).transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Sink runs before the equality short-circuits — the guard cannot retroactively
// sanitize `from`.
function badRequireGuardOrder(address from, address to, uint256 a) public {
require(token.transferFrom(from, to, a) && from == msg.sender); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badAssertGuardOrder(address from, address to, uint256 a) public {
assert(token.transferFrom(from, to, a) && from == msg.sender); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badShortCircuitReassignKillsSafe(bool flag, address from, address to, uint256 a) public {
address x = msg.sender;
bool ok = flag && ((x = from) != address(0));
ok;
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Modifier guard placed *after* `_;` cannot be hoisted.
modifier lateCheck(address f) {
_;
require(f == msg.sender, "auth");
}
function badModifierGuardAfterPlaceholder(
address from,
address to,
uint256 a
) public lateCheck(from) {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Multi-`_;` modifier is skipped (placeholder ordering can't be assumed sound).
modifier multiPlaceholder(address f) {
_;
require(f == msg.sender, "auth");
_;
}
function badModifierMultiPlaceholder(
address from,
address to,
uint256 a
) public multiPlaceholder(from) {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Loop body kills a previously-safe local.
function badLoopKillsSafeLocal(address from, address to, uint256 a) public {
address x = msg.sender;
for (uint256 i = 0; i < 1; i++) {
x = from;
}
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Try clause kills a previously-safe local.
function badTryClauseKillsSafeLocal(
address from,
address to,
uint256 a,
IERC20 t
) public {
address x = msg.sender;
try t.transfer(to, a) returns (bool) {
x = from;
} catch {}
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Mutable state vars are not transitive: storage may be rewritten before the sink.
function badViaStateVarGuard(address from, address to, uint256 a) public {
require(owner == msg.sender, "owner check");
require(from == owner, "from check");
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// State-var token reassigned after permit must invalidate the record.
function badPermitStateTokenReassigned(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(from, address(this), a, deadline, v, r, s);
token = other;
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// State-var owner reassigned after permit must invalidate the record.
function badPermitStateOwnerReassigned(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
owner = from;
token.permit(owner, address(this), a, deadline, v, r, s);
owner = to;
token.transferFrom(owner, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// State-var token reassigned on only one branch: post-`if` intersection drops the permit.
function badPermitStateTokenMaybeReassigned(
bool flag,
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(from, address(this), a, deadline, v, r, s);
if (flag) {
token = other;
}
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// -- NEGATIVE CASES (should NOT warn) --
function okMsgSender(address to, uint256 a) public {
token.transferFrom(msg.sender, to, a);
}
function okThis(address to, uint256 a) public {
token.transferFrom(address(this), to, a);
}
function okTransitive(address to, uint256 a) public {
address tmp = address(msg.sender);
token.transferFrom(tmp, to, a);
}
function okPayableCast(address to, uint256 a) public {
token.transferFrom(payable(msg.sender), to, a);
}
function okRequireEq(address from, address to, uint256 a) public {
require(from == msg.sender, "auth");
token.transferFrom(from, to, a);
}
function okAssertEq(address from, address to, uint256 a) public {
assert(from == msg.sender);
token.transferFrom(from, to, a);
}
function okConjunction(address from, address to, uint256 a) public {
require(from == msg.sender && to != address(0), "auth");
token.transferFrom(from, to, a);
}
// Short-circuit: the equality holds by the time the sink runs.
function okRequireShortCircuit(address from, address to, uint256 a) public {
require(from == msg.sender && token.transferFrom(from, to, a));
}
function okIfRevert(address from, address to, uint256 a) public {
if (from != msg.sender) revert("auth");
token.transferFrom(from, to, a);
}
function okParens(address to, uint256 a) public {
token.transferFrom((msg.sender), to, a);
}
function okTernaryBothSafe(bool flag, address to, uint256 a) public {
address x = flag ? msg.sender : address(this);
token.transferFrom(x, to, a);
}
function okTuple(address to, uint256 a, address ignored) public {
(address x, ) = (msg.sender, ignored);
token.transferFrom(x, to, a);
}
modifier onlySelf(address f) {
require(f == msg.sender, "auth");
_;
}
function okModifier(address from, address to, uint256 a) public onlySelf(from) {
token.transferFrom(from, to, a);
}
// Mutable storage modifier-arg reassigned before the sink.
function badModifierMutableState(address from, address to, uint256 a) public onlySelf(owner) {
owner = from;
token.transferFrom(owner, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// OpenZeppelin's `_msgSender()` resolves to `msg.sender`.
function okMsgSenderHelper(address to, uint256 a) public {
token.transferFrom(_msgSender(), to, a);
}
// Helper chain `_origin -> _msgSender -> msg.sender` within depth budget.
function okMsgSenderHelperChain(address to, uint256 a) public {
token.transferFrom(_origin(), to, a);
}
function okMsgSenderHelperGuard(address from, address to, uint256 a) public {
require(from == _msgSender(), "auth");
token.transferFrom(from, to, a);
}
// `assert(false)` is recognised as an exit.
function okIfAssertFalse(address from, address to, uint256 a) public {
if (from != msg.sender) {
assert(false);
}
token.transferFrom(from, to, a);
}
// `immutable` state vars can chain: storage cannot be rewritten post-deploy.
function okImmutableOwnerChain(address from, address to, uint256 a) public {
require(trustedOwner == msg.sender, "owner");
require(from == trustedOwner, "from");
token.transferFrom(from, to, a);
}
// Same token, spender == this, owner matches `from`, no reassignment.
function okPermit(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(from, address(this), a, deadline, v, r, s);
token.transferFrom(from, to, a);
}
// `from` round-tripped through a numeric cast (address -> uint160 -> address) must still
// resolve back to the same underlying variable so the permit correlates with the pull.
function okPermitNumericCastFrom(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(address(uint160(from)), address(this), a, deadline, v, r, s);
token.transferFrom(address(uint160(from)), to, a);
}
// Same numeric-cast round-trip, but the pull uses a *different* raw variable - must still warn.
function badPermitNumericCastFromMismatch(
address from,
address other_,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit(address(uint160(from)), address(this), a, deadline, v, r, s);
token.transferFrom(address(uint160(other_)), to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// ERC721 same-named methods must NOT trigger this lint.
function okErc721TransferFrom(address from, address to, uint256 id) public {
nft.transferFrom(from, to, id);
nft.safeTransferFrom(from, to, id);
nft.safeTransferFrom(from, to, id, "");
}
// View / pure functions are out of scope.
function viewMethodsExempt(address from, address to, uint256 a) external view returns (uint256) {
from;
to;
a;
return 0;
}
// Reassignment that *establishes* safety.
function okReassignToSafe(address from, address to, uint256 a) public {
address x = from;
x = msg.sender;
token.transferFrom(x, to, a);
}
function okLibrarySelf(address to, uint256 a) public {
SafeERC20.safeTransferFrom(token, address(this), to, a);
}
// Explicit `else` inherits the negated guard.
function okExplicitElseGuard(address from, address to, uint256 a) public {
if (from != msg.sender) {
revert("auth");
} else {
token.transferFrom(from, to, a);
}
}
// EIP-3156 lender repayment: receiver is trusted after `.onFlashLoan(...)`.
function okFlashLender(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public returns (bool) {
token.transfer(address(receiver), amount);
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), address(this), amount + fee);
return true;
}
function badTupleReassignKillsSafe(address from, address to, uint256 a) public {
address x = msg.sender;
address y;
(x, y) = (from, to);
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badFlashLoanInBranch(
bool flag,
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
if (flag) {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
}
token.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badFlashLoanReceiverReassigned(
IERC3156FlashBorrower receiver,
IERC3156FlashBorrower untrusted,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
receiver = untrusted;
token.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badFakeFlashLoan(
IFakeFlashBorrower fake,
address to,
uint256 a,
bytes calldata data
) public {
fake.onFlashLoan(data);
token.transferFrom(address(fake), to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Pull-back token differs from the one passed to the hook.
function badFlashLoanWrongToken(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
other.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Pull-back recipient isn't the lender.
function badFlashLoanWrongRecipient(
IERC3156FlashBorrower receiver,
address attacker,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), attacker, amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Pull-back amount isn't `amount + fee`.
function badFlashLoanWrongAmount(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
uint256 other,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), address(this), other); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// The callback commits to a truncated amount (via a narrowing cast); the pull-back still
// claims the full untruncated `amount + fee`. Peeling the numeric cast for amount/fee must
// stay narrow (no cast-peeling at all), or this would be wrongly treated as matching.
function badFlashLoanCallbackAmountTruncated(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), uint160(amount), fee, data);
token.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Same hazard, mirrored: full callback amount, but the pull-back sums a locally truncated
// stand-in for the fee.
function badFlashLoanPullFeeTruncated(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), address(this), amount + uint256(uint160(fee))); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Same hazard again, but through the `sum_of` local-alias fallback rather than a direct
// `amount + fee` expression: the pull-back passes a truncated stand-in for the local that
// holds the real sum.
function badFlashLoanSumOfLocalTruncated(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
uint256 total = amount + fee;
token.transferFrom(address(receiver), address(this), uint256(uint160(total))); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Second pull-back after the obligation has been consumed.
function badFlashLoanDoublePull(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), address(this), amount + fee);
token.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// `fee + amount` (commuted) still matches.
function okFlashLoanCommutativeAmount(
IERC3156FlashBorrower receiver,
uint256 amount,
uint256 fee,
bytes calldata data
) public {
receiver.onFlashLoan(msg.sender, address(token), amount, fee, data);
token.transferFrom(address(receiver), address(this), fee + amount);
}
function badNamedMember(address from, address to, uint256 a) public {
token.transferFrom({from: from, to: to, amount: a}); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badNamedLibrary(address from, address to, uint256 a) public {
SafeERC20.safeTransferFrom({token: token, from: from, to: to, value: a}); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function okNamedMemberSender(address to, uint256 a) public {
token.transferFrom({from: msg.sender, to: to, amount: a});
}
function okNamedLibrarySelf(address to, uint256 a) public {
SafeERC20.safeTransferFrom({token: token, from: address(this), to: to, value: a});
}
function okNamedPermit(
address from,
address to,
uint256 a,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
token.permit({
owner: from,
spender: address(this),
value: a,
deadline: deadline,
v: v,
r: r,
s: s
});
token.transferFrom({from: from, to: to, amount: a});
}
// `delete` clears a prior safe-fact.
function badDeleteKillsSafe(address from, address to, uint256 a) public {
address x = msg.sender;
delete x;
x = from;
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Flash-loan call on the RHS of `&&` may not execute; its repayment must not leak.
function badFlashLoanShortCircuit(
bool flag,
IERC3156FlashBorrower receiver,
bytes32 MAGIC,
uint256 amount,
uint256 fee,
bytes calldata data
) public returns (bool) {
bool ok = flag
&& receiver.onFlashLoan(msg.sender, address(token), amount, fee, data) == MAGIC;
token.transferFrom(address(receiver), address(this), amount + fee); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
return ok;
}
// `do-while` body runs at least once — facts established inside flow out.
function okDoWhileEstablishesSafe(address from, address to, uint256 a) public {
address x = from;
do {
x = msg.sender;
} while (false);
token.transferFrom(x, to, a);
}
// `break` may skip the safe assignment.
function badDoWhileBreakSkipsSafe(address from, address to, uint256 a, bool flag) public {
address x = from;
do {
if (flag) break;
x = msg.sender;
} while (false);
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badDoWhileBreakSkipsResafe(address from, address to, uint256 a, bool flag) public {
address x = msg.sender;
do {
x = from;
if (flag) break;
x = msg.sender;
} while (false);
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// `continue` may also skip the safe assignment.
function badDoWhileContinueSkipsSafe(address from, address to, uint256 a, bool flag) public {
address x = from;
do {
if (flag) continue;
x = msg.sender;
} while (false);
token.transferFrom(x, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
// Nested-loop `break` doesn't target the outer do-while.
function okDoWhileNestedLoopBreak(address from, address to, uint256 a) public {
address x = from;
do {
for (uint256 i = 0; i < 1; i++) {
if (i == 0) break;
}
x = msg.sender;
} while (false);
token.transferFrom(x, to, a);
}
// -- MODIFIER BODY SINKS --
modifier pullBad(address from, address to, uint256 a) {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
_;
}
function modifierSinkBad(address from, address to, uint256 a) public pullBad(from, to, a) {}
modifier guardedPullOk(address from, address to, uint256 a) {
require(from == msg.sender, "auth");
token.transferFrom(from, to, a);
_;
}
function modifierGuardedSinkOk(address from, address to, uint256 a) public guardedPullOk(from, to, a) {}
modifier pullBeforeGuardBad(address from, address to, uint256 a) {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
require(from == msg.sender, "auth");
_;
}
function modifierSinkBeforeGuardBad(address from, address to, uint256 a)
public
pullBeforeGuardBad(from, to, a)
{}
// A modifier is only reachable through its invocations, so a `from` that is safe at every
// invocation site is not arbitrary.
modifier pullFromCallerOk(address from, address to, uint256 a) {
token.transferFrom(from, to, a);
_;
}
function modifierSenderInvocationOk(address to, uint256 a) public pullFromCallerOk(msg.sender, to, a) {}
function modifierSelfInvocationOk(address to, uint256 a) public pullFromCallerOk(address(this), to, a) {}
modifier pullFromMixedCallersBad(address from, address to, uint256 a) {
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
_;
}
function modifierMixedSenderInvocation(address to, uint256 a) public pullFromMixedCallersBad(msg.sender, to, a) {}
function modifierMixedArbitraryInvocation(address from, address to, uint256 a)
public
pullFromMixedCallersBad(from, to, a)
{}
// Statements after `_;` keep the prefix facts: parameters and locals cannot be changed by the
// wrapped function body, and mutable state is never trusted in the first place.
modifier guardedSuffixPullOk(address from, address to, uint256 a) {
require(from == msg.sender, "auth");
_;
token.transferFrom(from, to, a);
}
function modifierGuardedSuffixOk(address from, address to, uint256 a)
public
guardedSuffixPullOk(from, to, a)
{}
modifier suffixPullBad(address from, address to, uint256 a) {
_;
token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function modifierSuffixSinkBad(address from, address to, uint256 a) public suffixPullBad(from, to, a) {}
// -- FALLBACK SINKS --
// Only constructors are excluded, so a sink reachable through `fallback`/`receive` is
// reported like one in an ordinary function.
fallback(bytes calldata data) external returns (bytes memory) {
address from = abi.decode(data, (address));
token.transferFrom(from, msg.sender, 1); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
return "";
}
}
// Struct / array / mapping receivers.
contract ContainerReceivers {
struct Config {
IERC20 token;
}
Config cfg;
IERC20[] tokens;
mapping(uint256 => IERC20) tokenMap;
function badStructFieldReceiver(address from, address to, uint256 a) public {
cfg.token.transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badArrayElementReceiver(address from, address to, uint256 a) public {
tokens[0].transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function badMappingValueReceiver(uint256 id, address from, address to, uint256 a) public {
tokenMap[id].transferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function okStructFieldSender(address to, uint256 a) public {
cfg.token.transferFrom(msg.sender, to, a);
}
}
// Solady-style: first param is `address`, not a contract type.
library SafeTransferLib {
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
token; from; to; amount; // body intentionally elided.
}
}
contract SoladyCallSites {
address token;
function badSolady(address from, address to, uint256 a) public {
SafeTransferLib.safeTransferFrom(token, from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function okSoladySender(address to, uint256 a) public {
SafeTransferLib.safeTransferFrom(token, msg.sender, to, a);
}
function okSoladySelf(address to, uint256 a) public {
SafeTransferLib.safeTransferFrom(token, address(this), to, a);
}
}
contract InternalForwardedPulls {
address token;
function okDeposit(address to, uint256 a) public {
_pull(msg.sender, to, a);
}
function okMint(address to, uint256 a) public {
_pull(payable(msg.sender), to, a);
}
function badForward(address from, address to, uint256 a) public {
_mixedPull(from, to, a);
}
function okForward(address to, uint256 a) public {
_mixedPull(msg.sender, to, a);
}
function _pull(address from, address to, uint256 a) internal {
SafeTransferLib.safeTransferFrom(token, from, to, a);
}
function _mixedPull(address from, address to, uint256 a) internal {
SafeTransferLib.safeTransferFrom(token, from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
}
// `using ... for address`: 3-arg `safeTransferFrom` member call on an `address`.
contract SoladyUsingForAddress {
using SafeTransferLib for address;
address token;
function badSoladyMember(address from, address to, uint256 a) public {
token.safeTransferFrom(from, to, a); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function okSoladyMemberSender(address to, uint256 a) public {
token.safeTransferFrom(msg.sender, to, a);
}
function okSoladyMemberSelf(address to, uint256 a) public {
token.safeTransferFrom(address(this), to, a);
}
function okSoladyMemberGuarded(address from, address to, uint256 a) public {
require(from == msg.sender, "auth");
token.safeTransferFrom(from, to, a);
}
function badNamedSolady(address from, address to, uint256 a) public {
SafeTransferLib.safeTransferFrom({token: token, from: from, to: to, amount: a}); //~WARN: `transferFrom` uses an arbitrary `from`; require it to equal `msg.sender` or `address(this)`
}
function okNamedSoladySender(address to, uint256 a) public {
SafeTransferLib.safeTransferFrom({token: token, from: msg.sender, to: to, amount: a});
}
}
// ERC721 helper shares the 4-arg shape but is not ERC20.
library SafeERC721 {
function safeTransferFrom(IERC721 nft, address from, address to, uint256 tokenId) internal {