mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	selftests/bpf: Test file_pos field in bpf_sysctl ctx
Test access to file_pos field of bpf_sysctl context, both read (incl. narrow read) and write. # ./test_sysctl ... Test case: ctx:file_pos sysctl:read read ok .. [PASS] Test case: ctx:file_pos sysctl:read read ok narrow .. [PASS] Test case: ctx:file_pos sysctl:read write ok .. [PASS] ... Signed-off-by: Andrey Ignatov <rdna@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
		
							parent
							
								
									786047dd08
								
							
						
					
					
						commit
						9a1027e525
					
				
					 1 changed files with 64 additions and 0 deletions
				
			
		| 
						 | 
					@ -30,6 +30,7 @@ struct sysctl_test {
 | 
				
			||||||
	const char *sysctl;
 | 
						const char *sysctl;
 | 
				
			||||||
	int open_flags;
 | 
						int open_flags;
 | 
				
			||||||
	const char *newval;
 | 
						const char *newval;
 | 
				
			||||||
 | 
						const char *oldval;
 | 
				
			||||||
	enum {
 | 
						enum {
 | 
				
			||||||
		LOAD_REJECT,
 | 
							LOAD_REJECT,
 | 
				
			||||||
		ATTACH_REJECT,
 | 
							ATTACH_REJECT,
 | 
				
			||||||
| 
						 | 
					@ -130,6 +131,64 @@ static struct sysctl_test tests[] = {
 | 
				
			||||||
		.open_flags = O_RDONLY,
 | 
							.open_flags = O_RDONLY,
 | 
				
			||||||
		.result = LOAD_REJECT,
 | 
							.result = LOAD_REJECT,
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							.descr = "ctx:file_pos sysctl:read read ok",
 | 
				
			||||||
 | 
							.insns = {
 | 
				
			||||||
 | 
								/* If (file_pos == X) */
 | 
				
			||||||
 | 
								BPF_LDX_MEM(BPF_W, BPF_REG_7, BPF_REG_1,
 | 
				
			||||||
 | 
									    offsetof(struct bpf_sysctl, file_pos)),
 | 
				
			||||||
 | 
								BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0, 2),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								/* return ALLOW; */
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 1),
 | 
				
			||||||
 | 
								BPF_JMP_A(1),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								/* else return DENY; */
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 0),
 | 
				
			||||||
 | 
								BPF_EXIT_INSN(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							.attach_type = BPF_CGROUP_SYSCTL,
 | 
				
			||||||
 | 
							.sysctl = "kernel/ostype",
 | 
				
			||||||
 | 
							.open_flags = O_RDONLY,
 | 
				
			||||||
 | 
							.result = SUCCESS,
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							.descr = "ctx:file_pos sysctl:read read ok narrow",
 | 
				
			||||||
 | 
							.insns = {
 | 
				
			||||||
 | 
								/* If (file_pos == X) */
 | 
				
			||||||
 | 
								BPF_LDX_MEM(BPF_B, BPF_REG_7, BPF_REG_1,
 | 
				
			||||||
 | 
									    offsetof(struct bpf_sysctl, file_pos)),
 | 
				
			||||||
 | 
								BPF_JMP_IMM(BPF_JNE, BPF_REG_7, 0, 2),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								/* return ALLOW; */
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 1),
 | 
				
			||||||
 | 
								BPF_JMP_A(1),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								/* else return DENY; */
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 0),
 | 
				
			||||||
 | 
								BPF_EXIT_INSN(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							.attach_type = BPF_CGROUP_SYSCTL,
 | 
				
			||||||
 | 
							.sysctl = "kernel/ostype",
 | 
				
			||||||
 | 
							.open_flags = O_RDONLY,
 | 
				
			||||||
 | 
							.result = SUCCESS,
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							.descr = "ctx:file_pos sysctl:read write ok",
 | 
				
			||||||
 | 
							.insns = {
 | 
				
			||||||
 | 
								/* file_pos = X */
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 2),
 | 
				
			||||||
 | 
								BPF_STX_MEM(BPF_W, BPF_REG_1, BPF_REG_0,
 | 
				
			||||||
 | 
									    offsetof(struct bpf_sysctl, file_pos)),
 | 
				
			||||||
 | 
								BPF_MOV64_IMM(BPF_REG_0, 1),
 | 
				
			||||||
 | 
								BPF_EXIT_INSN(),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							.attach_type = BPF_CGROUP_SYSCTL,
 | 
				
			||||||
 | 
							.sysctl = "kernel/ostype",
 | 
				
			||||||
 | 
							.open_flags = O_RDONLY,
 | 
				
			||||||
 | 
							.oldval = "nux\n",
 | 
				
			||||||
 | 
							.result = SUCCESS,
 | 
				
			||||||
 | 
						},
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		.descr = "sysctl_get_name sysctl_value:base ok",
 | 
							.descr = "sysctl_get_name sysctl_value:base ok",
 | 
				
			||||||
		.insns = {
 | 
							.insns = {
 | 
				
			||||||
| 
						 | 
					@ -848,6 +907,11 @@ static int access_sysctl(const char *sysctl_path,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (read(fd, buf, sizeof(buf)) == -1)
 | 
							if (read(fd, buf, sizeof(buf)) == -1)
 | 
				
			||||||
			goto err;
 | 
								goto err;
 | 
				
			||||||
 | 
							if (test->oldval &&
 | 
				
			||||||
 | 
							    strncmp(buf, test->oldval, strlen(test->oldval))) {
 | 
				
			||||||
 | 
								log_err("Read value %s != %s", buf, test->oldval);
 | 
				
			||||||
 | 
								goto err;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else if (test->open_flags == O_WRONLY) {
 | 
						} else if (test->open_flags == O_WRONLY) {
 | 
				
			||||||
		if (!test->newval) {
 | 
							if (!test->newval) {
 | 
				
			||||||
			log_err("New value for sysctl is not set");
 | 
								log_err("New value for sysctl is not set");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue