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
// SPDX-License-Identifier: GPL-2.0
/* Converted from tools/testing/selftests/bpf/verifier/ctx.c */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
#include "../test_kmods/bpf_testmod_kfunc.h"

static const char ctx_strncmp_target[] = "ctx";
static const char ctx_snprintf_fmt[] = "";

SEC("tc")
__description("context stores via BPF_ATOMIC")
__failure __msg("BPF_ATOMIC stores into R1 ctx is not allowed")
__naked void context_stores_via_bpf_atomic(void)
{
	asm volatile ("					\
	r0 = 0;						\
	lock *(u32 *)(r1 + %[__sk_buff_mark]) += w0;	\
	exit;						\
"	:
	: __imm_const(__sk_buff_mark, offsetof(struct __sk_buff, mark))
	: __clobber_all);
}

SEC("tc")
__description("arithmetic ops make PTR_TO_CTX unusable")
__failure __msg("dereference of modified ctx ptr")
__naked void make_ptr_to_ctx_unusable(void)
{
	asm volatile ("					\
	r1 += %[__imm_0];				\
	r0 = *(u32*)(r1 + %[__sk_buff_mark]);		\
	exit;						\
"	:
	: __imm_const(__imm_0,
		      offsetof(struct __sk_buff, data) - offsetof(struct __sk_buff, mark)),
	  __imm_const(__sk_buff_mark, offsetof(struct __sk_buff, mark))
	: __clobber_all);
}

SEC("tc")
__description("pass unmodified ctx pointer to helper")
__success __retval(0)
__naked void unmodified_ctx_pointer_to_helper(void)
{
	asm volatile ("					\
	r2 = 0;						\
	call %[bpf_csum_update];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_csum_update)
	: __clobber_all);
}

SEC("tc")
__description("pass modified ctx pointer to helper, 1")
__failure __msg("negative offset ctx ptr R1 off=-612 disallowed")
__naked void ctx_pointer_to_helper_1(void)
{
	asm volatile ("					\
	r1 += -612;					\
	r2 = 0;						\
	call %[bpf_csum_update];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_csum_update)
	: __clobber_all);
}

SEC("socket")
__description("pass modified ctx pointer to helper, 2")
__failure __msg("negative offset ctx ptr R1 off=-612 disallowed")
__naked void ctx_pointer_to_helper_2(void)
{
	asm volatile ("					\
	r1 += -612;					\
	call %[bpf_get_socket_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_socket_cookie)
	: __clobber_all);
}

SEC("tc")
__description("pass modified ctx pointer to helper, 3")
__failure __msg("variable ctx access var_off=(0x0; 0x4)")
__naked void ctx_pointer_to_helper_3(void)
{
	asm volatile ("					\
	r3 = *(u32*)(r1 + 0);				\
	r3 &= 4;					\
	r1 += r3;					\
	r2 = 0;						\
	call %[bpf_csum_update];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_csum_update)
	: __clobber_all);
}

SEC("cgroup/sendmsg6")
__description("pass ctx or null check, 1: ctx")
__success
__naked void or_null_check_1_ctx(void)
{
	asm volatile ("					\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/sendmsg6")
__description("pass ctx or null check, 2: null")
__success
__naked void or_null_check_2_null(void)
{
	asm volatile ("					\
	r1 = 0;						\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/sendmsg6")
__description("pass ctx or null check, 3: 1")
__failure __msg("R1 type=scalar expected=ctx")
__naked void or_null_check_3_1(void)
{
	asm volatile ("					\
	r1 = 1;						\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/sendmsg6")
__description("pass ctx or null check, 4: ctx - const")
__failure __msg("negative offset ctx ptr R1 off=-612 disallowed")
__naked void null_check_4_ctx_const(void)
{
	asm volatile ("					\
	r1 += -612;					\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/connect4")
__description("pass ctx or null check, 5: null (connect)")
__success
__naked void null_check_5_null_connect(void)
{
	asm volatile ("					\
	r1 = 0;						\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/post_bind4")
__description("pass ctx or null check, 6: null (bind)")
__success
__naked void null_check_6_null_bind(void)
{
	asm volatile ("					\
	r1 = 0;						\
	call %[bpf_get_netns_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_netns_cookie)
	: __clobber_all);
}

SEC("cgroup/post_bind4")
__description("pass ctx or null check, 7: ctx (bind)")
__success
__naked void null_check_7_ctx_bind(void)
{
	asm volatile ("					\
	call %[bpf_get_socket_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_socket_cookie)
	: __clobber_all);
}

SEC("cgroup/post_bind4")
__description("pass ctx or null check, 8: null (bind)")
__failure __msg("R1 type=scalar expected=ctx")
__naked void null_check_8_null_bind(void)
{
	asm volatile ("					\
	r1 = 0;						\
	call %[bpf_get_socket_cookie];			\
	r0 = 0;						\
	exit;						\
"	:
	: __imm(bpf_get_socket_cookie)
	: __clobber_all);
}

#define narrow_load(type, ctx, field)					\
	SEC(type)							\
	__description("narrow load on field " #field " of " #ctx)	\
	__failure __msg("invalid bpf_context access")			\
	__naked void invalid_narrow_load##ctx##field(void)		\
	{								\
		asm volatile ("						\
		r1 = *(u32 *)(r1 + %[off]);				\
		r0 = 0;							\
		exit;"							\
		:							\
		: __imm_const(off, offsetof(struct ctx, field) + 4)	\
		: __clobber_all);					\
	}

narrow_load("cgroup/getsockopt", bpf_sockopt, sk);
narrow_load("cgroup/getsockopt", bpf_sockopt, optval);
narrow_load("cgroup/getsockopt", bpf_sockopt, optval_end);
narrow_load("tc", __sk_buff, sk);
narrow_load("cgroup/bind4", bpf_sock_addr, sk);
narrow_load("sockops", bpf_sock_ops, sk);
narrow_load("sockops", bpf_sock_ops, skb_data);
narrow_load("sockops", bpf_sock_ops, skb_data_end);
narrow_load("sockops", bpf_sock_ops, skb_hwtstamp);

#define unaligned_access(type, ctx, field)					\
	SEC(type)								\
	__description("unaligned access on field " #field " of " #ctx)		\
	__failure __msg("invalid bpf_context access")				\
	__naked void unaligned_ctx_access_##ctx##field(void)			\
	{									\
		asm volatile ("							\
		r1 = *(u%[size] *)(r1 + %[off]);				\
		r0 = 0;								\
		exit;"								\
		:								\
		: __imm_const(size, sizeof_field(struct ctx, field) * 8),	\
		  __imm_const(off, offsetof(struct ctx, field) + 1)		\
		: __clobber_all);						\
	}

unaligned_access("flow_dissector", __sk_buff, data);
unaligned_access("netfilter", bpf_nf_ctx, skb);

#define padding_access(type, ctx, prev_field, sz)			\
	SEC(type)							\
	__description("access on " #ctx " padding after " #prev_field)	\
	__naked void padding_ctx_access_##ctx(void)			\
	{								\
		asm volatile ("						\
		r1 = *(u%[size] *)(r1 + %[off]);			\
		r0 = 0;							\
		exit;"							\
		:							\
		: __imm_const(size, sz * 8),				\
		  __imm_const(off, offsetofend(struct ctx, prev_field))	\
		: __clobber_all);					\
	}

__failure __msg("invalid bpf_context access")
padding_access("cgroup/bind4", bpf_sock_addr, msg_src_ip6[3], 4);

__success
padding_access("sk_lookup", bpf_sk_lookup, remote_port, 2);

__failure __msg("invalid bpf_context access")
padding_access("tc", __sk_buff, tstamp_type, 2);

__failure __msg("invalid bpf_context access")
padding_access("cgroup/post_bind4", bpf_sock, dst_port, 2);

__failure __msg("invalid bpf_context access")
padding_access("sk_reuseport", sk_reuseport_md, hash, 4);

SEC("?syscall")
__description("syscall: write to ctx with fixed offset")
__success
int syscall_ctx_fixed_off_write(void *ctx)
{
	char *p = ctx;

	*(__u32 *)p = 0;
	*(__u32 *)(p + 4) = 0;
	return 0;
}

SEC("?syscall")
__description("syscall: read ctx with fixed offset")
__success
int syscall_ctx_fixed_off_read(void *ctx)
{
	char *p = ctx;
	volatile __u32 val;

	val = *(__u32 *)(p + 4);
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: unaligned read ctx with fixed offset")
__success
int syscall_ctx_unaligned_fixed_off_read(void *ctx)
{
	char *p = ctx;
	volatile __u32 val;

	val = *(__u32 *)(p + 2);
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: unaligned write ctx with fixed offset")
__success
int syscall_ctx_unaligned_fixed_off_write(void *ctx)
{
	char *p = ctx;

	*(__u32 *)(p + 2) = 0;
	return 0;
}

SEC("?syscall")
__description("syscall: read ctx with variable offset")
__success
int syscall_ctx_var_off_read(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;
	volatile __u32 val;

	off &= 0xfc;
	p += off;
	val = *(__u32 *)p;
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: write ctx with variable offset")
__success
int syscall_ctx_var_off_write(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	p += off;
	*(__u32 *)p = 0;
	return 0;
}

SEC("?syscall")
__description("syscall: unaligned read ctx with variable offset")
__success
int syscall_ctx_unaligned_var_off_read(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;
	volatile __u32 val;

	off &= 0xfc;
	off += 2;
	p += off;
	val = *(__u32 *)p;
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: unaligned write ctx with variable offset")
__success
int syscall_ctx_unaligned_var_off_write(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	off += 2;
	p += off;
	*(__u32 *)p = 0;
	return 0;
}

SEC("?syscall")
__description("syscall: reject ctx access past U16_MAX with fixed offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_u16_max_fixed_off(void *ctx)
{
	char *p = ctx;
	volatile __u32 val;

	p += 65535;
	val = *(__u32 *)p;
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: reject ctx access past U16_MAX with variable offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_u16_max_var_off(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;
	volatile __u32 val;

	off &= 0xffff;
	off += 1;
	p += off;
	val = *(__u32 *)p;
	(void)val;
	return 0;
}

SEC("?syscall")
__description("syscall: reject negative variable offset ctx access")
__failure __msg("min value is negative")
int syscall_ctx_neg_var_off(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 4;
	p -= off;
	return *(__u32 *)p;
}

SEC("?syscall")
__description("syscall: reject unbounded variable offset ctx access")
__failure __msg("unbounded memory access")
int syscall_ctx_unbounded_var_off(void *ctx)
{
	__u64 off = (__u32)bpf_get_prandom_u32();
	char *p = ctx;

	off <<= 2;
	p += off;
	return *(__u32 *)p;
}

SEC("?syscall")
__description("syscall: helper read ctx with fixed offset")
__success
int syscall_ctx_helper_fixed_off_read(void *ctx)
{
	char *p = ctx;

	p += 4;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: helper write ctx with fixed offset")
__success
int syscall_ctx_helper_fixed_off_write(void *ctx)
{
	char *p = ctx;

	p += 4;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: helper unaligned read ctx with fixed offset")
__success
int syscall_ctx_helper_unaligned_fixed_off_read(void *ctx)
{
	char *p = ctx;

	p += 2;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: helper unaligned write ctx with fixed offset")
__success
int syscall_ctx_helper_unaligned_fixed_off_write(void *ctx)
{
	char *p = ctx;

	p += 2;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: helper read ctx with variable offset")
__success
int syscall_ctx_helper_var_off_read(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	p += off;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: helper write ctx with variable offset")
__success
int syscall_ctx_helper_var_off_write(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	p += off;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: helper unaligned read ctx with variable offset")
__success
int syscall_ctx_helper_unaligned_var_off_read(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	off += 2;
	p += off;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: helper unaligned write ctx with variable offset")
__success
int syscall_ctx_helper_unaligned_var_off_write(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	off += 2;
	p += off;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: reject helper read ctx past U16_MAX with fixed offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_helper_u16_max_fixed_off_read(void *ctx)
{
	char *p = ctx;

	p += 65535;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: reject helper write ctx past U16_MAX with fixed offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_helper_u16_max_fixed_off_write(void *ctx)
{
	char *p = ctx;

	p += 65535;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: reject helper read ctx past U16_MAX with variable offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_helper_u16_max_var_off_read(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xffff;
	off += 1;
	p += off;
	return bpf_strncmp(p, 4, ctx_strncmp_target);
}

SEC("?syscall")
__description("syscall: reject helper write ctx past U16_MAX with variable offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_helper_u16_max_var_off_write(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xffff;
	off += 1;
	p += off;
	return bpf_probe_read_kernel(p, 4, 0);
}

SEC("?syscall")
__description("syscall: helper read zero-sized ctx access")
__success
int syscall_ctx_helper_zero_sized_read(void *ctx)
{
	return bpf_snprintf(0, 0, ctx_snprintf_fmt, ctx, 0);
}

SEC("?syscall")
__description("syscall: helper write zero-sized ctx access")
__success
int syscall_ctx_helper_zero_sized_write(void *ctx)
{
	return bpf_probe_read_kernel(ctx, 0, 0);
}

SEC("?syscall")
__description("syscall: kfunc access ctx with fixed offset")
__success
int syscall_ctx_kfunc_fixed_off(void *ctx)
{
	char *p = ctx;

	p += 4;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: kfunc access ctx with variable offset")
__success
int syscall_ctx_kfunc_var_off(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	p += off;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: kfunc unaligned access ctx with fixed offset")
__success
int syscall_ctx_kfunc_unaligned_fixed_off(void *ctx)
{
	char *p = ctx;

	p += 2;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: kfunc unaligned access ctx with variable offset")
__success
int syscall_ctx_kfunc_unaligned_var_off(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xfc;
	off += 2;
	p += off;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: reject kfunc ctx access past U16_MAX with fixed offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_kfunc_u16_max_fixed_off(void *ctx)
{
	char *p = ctx;

	p += 65535;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: reject kfunc ctx access past U16_MAX with variable offset")
__failure __msg("outside of the allowed memory range")
int syscall_ctx_kfunc_u16_max_var_off(void *ctx)
{
	__u64 off = bpf_get_prandom_u32();
	char *p = ctx;

	off &= 0xffff;
	off += 1;
	p += off;
	bpf_kfunc_call_test_mem_len_pass1(p, 4);
	return 0;
}

SEC("?syscall")
__description("syscall: kfunc access zero-sized ctx")
__success
int syscall_ctx_kfunc_zero_sized(void *ctx)
{
	bpf_kfunc_call_test_mem_len_pass1(ctx, 0);
	return 0;
}

/*
 * For non-syscall program types without convert_ctx_access, direct ctx
 * dereference is still allowed after adding a fixed offset, while variable
 * and negative direct accesses reject.
 *
 * Passing ctx as a helper or kfunc memory argument is only permitted for
 * syscall programs, so the helper and kfunc cases below validate rejection
 * for non-syscall ctx pointers at fixed, variable, and zero-sized accesses.
 */
#define no_rewrite_ctx_access(type, name, off, load_t)			\
	SEC("?" type)							\
	__description(type ": read ctx at fixed offset")		\
	__success							\
	int no_rewrite_##name##_fixed(void *ctx)			\
	{								\
		char *p = ctx;						\
		volatile load_t val;					\
									\
		val = *(load_t *)(p + off);				\
		(void)val;						\
		return 0;						\
	}								\
	SEC("?" type)							\
	__description(type ": reject variable offset ctx access")	\
	__failure __msg("variable ctx access var_off=")			\
	int no_rewrite_##name##_var(void *ctx)			\
	{								\
		__u64 off_var = bpf_get_prandom_u32();			\
		char *p = ctx;						\
									\
		off_var &= 4;						\
		p += off_var;						\
		return *(load_t *)p;					\
	}								\
	SEC("?" type)							\
	__description(type ": reject negative offset ctx access")	\
	__failure __msg("invalid bpf_context access")			\
	int no_rewrite_##name##_neg(void *ctx)			\
	{								\
		char *p = ctx;						\
									\
		p -= 612;						\
		return *(load_t *)p;					\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper read ctx at fixed offset")	\
	__failure __msg("dereference of modified ctx ptr")		\
	int no_rewrite_##name##_helper_read_fixed(void *ctx)		\
	{								\
		char *p = ctx;						\
									\
		p += off;						\
		return bpf_strncmp(p, 4, ctx_strncmp_target);		\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper write ctx at fixed offset")	\
	__failure __msg("dereference of modified ctx ptr")		\
	int no_rewrite_##name##_helper_write_fixed(void *ctx)		\
	{								\
		char *p = ctx;						\
									\
		p += off;						\
		return bpf_probe_read_kernel(p, 4, 0);			\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper read ctx with variable offset") \
	__failure __msg("variable ctx access var_off=")			\
	int no_rewrite_##name##_helper_read_var(void *ctx)		\
	{								\
		__u64 off_var = bpf_get_prandom_u32();			\
		char *p = ctx;						\
									\
		off_var &= 4;						\
		p += off_var;						\
		return bpf_strncmp(p, 4, ctx_strncmp_target);		\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper write ctx with variable offset") \
	__failure __msg("variable ctx access var_off=")			\
	int no_rewrite_##name##_helper_write_var(void *ctx)		\
	{								\
		__u64 off_var = bpf_get_prandom_u32();			\
		char *p = ctx;						\
									\
		off_var &= 4;						\
		p += off_var;						\
		return bpf_probe_read_kernel(p, 4, 0);			\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper read zero-sized ctx access") \
	__failure __msg("R4 type=ctx expected=fp")			\
	int no_rewrite_##name##_helper_read_zero(void *ctx)		\
	{								\
		return bpf_snprintf(0, 0, ctx_snprintf_fmt, ctx, 0);	\
	}								\
	SEC("?" type)							\
	__description(type ": reject helper write zero-sized ctx access") \
	__failure __msg("R1 type=ctx expected=fp")			\
	int no_rewrite_##name##_helper_write_zero(void *ctx)		\
	{								\
		return bpf_probe_read_kernel(ctx, 0, 0);			\
	}								\
	SEC("?" type)							\
	__description(type ": reject kfunc ctx at fixed offset")	\
	__failure __msg("dereference of modified ctx ptr")		\
	int no_rewrite_##name##_kfunc_fixed(void *ctx)		\
	{								\
		char *p = ctx;						\
									\
		p += off;						\
		bpf_kfunc_call_test_mem_len_pass1(p, 4);		\
		return 0;						\
	}								\
	SEC("?" type)							\
	__description(type ": reject kfunc ctx with variable offset")	\
	__failure __msg("variable ctx access var_off=")			\
	int no_rewrite_##name##_kfunc_var(void *ctx)			\
	{								\
		__u64 off_var = bpf_get_prandom_u32();			\
		char *p = ctx;						\
									\
		off_var &= 4;						\
		p += off_var;						\
		bpf_kfunc_call_test_mem_len_pass1(p, 4);		\
		return 0;						\
	}								\
	SEC("?" type)							\
	__description(type ": reject kfunc zero-sized ctx access")	\
	__failure __msg("R1 type=ctx expected=fp")			\
	int no_rewrite_##name##_kfunc_zero(void *ctx)			\
	{								\
		bpf_kfunc_call_test_mem_len_pass1(ctx, 0);		\
		return 0;						\
	}

no_rewrite_ctx_access("kprobe", kprobe, 8, u64);
no_rewrite_ctx_access("tracepoint", tp, 8, u64);
no_rewrite_ctx_access("raw_tp", raw_tp, 8, u64);
no_rewrite_ctx_access("raw_tracepoint.w", raw_tp_w, 8, u64);
no_rewrite_ctx_access("fentry/bpf_modify_return_test", fentry, 8, u64);
no_rewrite_ctx_access("cgroup/dev", cgroup_dev, 4, u32);
no_rewrite_ctx_access("netfilter", netfilter, offsetof(struct bpf_nf_ctx, skb), u64);

char _license[] SEC("license") = "GPL";