Skip to content

Commit a0cb12b

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: Make sure optvals > PAGE_SIZE are bypassed
We are relying on the fact, that we can pass > sizeof(int) optvals to the SOL_IP+IP_FREEBIND option (the kernel will take first 4 bytes). In the BPF program we check that we can only touch PAGE_SIZE bytes, but the real optlen is PAGE_SIZE * 2. In both cases, we override it to some predefined value and trim the optlen. Also, let's modify exiting IP_TOS usecase to test optlen=0 case where BPF program just bypasses the data as is. Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200617010416.93086-2-sdf@google.com
1 parent d8fe449 commit a0cb12b

2 files changed

Lines changed: 91 additions & 9 deletions

File tree

‎tools/testing/selftests/bpf/prog_tests/sockopt_sk.c‎

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static int getsetsockopt(void)
1313
char cc[16]; /* TCP_CA_NAME_MAX */
1414
} buf = {};
1515
socklen_t optlen;
16+
char *big_buf = NULL;
1617

1718
fd = socket(AF_INET, SOCK_STREAM, 0);
1819
if (fd < 0) {
@@ -22,24 +23,31 @@ static int getsetsockopt(void)
2223

2324
/* IP_TOS - BPF bypass */
2425

25-
buf.u8[0] = 0x08;
26-
err = setsockopt(fd, SOL_IP, IP_TOS, &buf, 1);
26+
optlen = getpagesize() * 2;
27+
big_buf = calloc(1, optlen);
28+
if (!big_buf) {
29+
log_err("Couldn't allocate two pages");
30+
goto err;
31+
}
32+
33+
*(int *)big_buf = 0x08;
34+
err = setsockopt(fd, SOL_IP, IP_TOS, big_buf, optlen);
2735
if (err) {
2836
log_err("Failed to call setsockopt(IP_TOS)");
2937
goto err;
3038
}
3139

32-
buf.u8[0] = 0x00;
40+
memset(big_buf, 0, optlen);
3341
optlen = 1;
34-
err = getsockopt(fd, SOL_IP, IP_TOS, &buf, &optlen);
42+
err = getsockopt(fd, SOL_IP, IP_TOS, big_buf, &optlen);
3543
if (err) {
3644
log_err("Failed to call getsockopt(IP_TOS)");
3745
goto err;
3846
}
3947

40-
if (buf.u8[0] != 0x08) {
41-
log_err("Unexpected getsockopt(IP_TOS) buf[0] 0x%02x != 0x08",
42-
buf.u8[0]);
48+
if (*(int *)big_buf != 0x08) {
49+
log_err("Unexpected getsockopt(IP_TOS) optval 0x%x != 0x08",
50+
*(int *)big_buf);
4351
goto err;
4452
}
4553

@@ -78,6 +86,28 @@ static int getsetsockopt(void)
7886
goto err;
7987
}
8088

89+
/* IP_FREEBIND - BPF can't access optval past PAGE_SIZE */
90+
91+
optlen = getpagesize() * 2;
92+
memset(big_buf, 0, optlen);
93+
94+
err = setsockopt(fd, SOL_IP, IP_FREEBIND, big_buf, optlen);
95+
if (err != 0) {
96+
log_err("Failed to call setsockopt, ret=%d", err);
97+
goto err;
98+
}
99+
100+
err = getsockopt(fd, SOL_IP, IP_FREEBIND, big_buf, &optlen);
101+
if (err != 0) {
102+
log_err("Failed to call getsockopt, ret=%d", err);
103+
goto err;
104+
}
105+
106+
if (optlen != 1 || *(__u8 *)big_buf != 0x55) {
107+
log_err("Unexpected IP_FREEBIND getsockopt, optlen=%d, optval=0x%x",
108+
optlen, *(__u8 *)big_buf);
109+
}
110+
81111
/* SO_SNDBUF is overwritten */
82112

83113
buf.u32 = 0x01010101;
@@ -124,9 +154,11 @@ static int getsetsockopt(void)
124154
goto err;
125155
}
126156

157+
free(big_buf);
127158
close(fd);
128159
return 0;
129160
err:
161+
free(big_buf);
130162
close(fd);
131163
return -1;
132164
}

‎tools/testing/selftests/bpf/progs/sockopt_sk.c‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
char _license[] SEC("license") = "GPL";
99
__u32 _version SEC("version") = 1;
1010

11+
#ifndef PAGE_SIZE
12+
#define PAGE_SIZE 4096
13+
#endif
14+
1115
#define SOL_CUSTOM 0xdeadbeef
1216

1317
struct sockopt_sk {
@@ -28,12 +32,14 @@ int _getsockopt(struct bpf_sockopt *ctx)
2832
__u8 *optval = ctx->optval;
2933
struct sockopt_sk *storage;
3034

31-
if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
35+
if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
3236
/* Not interested in SOL_IP:IP_TOS;
3337
* let next BPF program in the cgroup chain or kernel
3438
* handle it.
3539
*/
40+
ctx->optlen = 0; /* bypass optval>PAGE_SIZE */
3641
return 1;
42+
}
3743

3844
if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
3945
/* Not interested in SOL_SOCKET:SO_SNDBUF;
@@ -51,6 +57,26 @@ int _getsockopt(struct bpf_sockopt *ctx)
5157
return 1;
5258
}
5359

60+
if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
61+
if (optval + 1 > optval_end)
62+
return 0; /* EPERM, bounds check */
63+
64+
ctx->retval = 0; /* Reset system call return value to zero */
65+
66+
/* Always export 0x55 */
67+
optval[0] = 0x55;
68+
ctx->optlen = 1;
69+
70+
/* Userspace buffer is PAGE_SIZE * 2, but BPF
71+
* program can only see the first PAGE_SIZE
72+
* bytes of data.
73+
*/
74+
if (optval_end - optval != PAGE_SIZE)
75+
return 0; /* EPERM, unexpected data size */
76+
77+
return 1;
78+
}
79+
5480
if (ctx->level != SOL_CUSTOM)
5581
return 0; /* EPERM, deny everything except custom level */
5682

@@ -81,12 +107,14 @@ int _setsockopt(struct bpf_sockopt *ctx)
81107
__u8 *optval = ctx->optval;
82108
struct sockopt_sk *storage;
83109

84-
if (ctx->level == SOL_IP && ctx->optname == IP_TOS)
110+
if (ctx->level == SOL_IP && ctx->optname == IP_TOS) {
85111
/* Not interested in SOL_IP:IP_TOS;
86112
* let next BPF program in the cgroup chain or kernel
87113
* handle it.
88114
*/
115+
ctx->optlen = 0; /* bypass optval>PAGE_SIZE */
89116
return 1;
117+
}
90118

91119
if (ctx->level == SOL_SOCKET && ctx->optname == SO_SNDBUF) {
92120
/* Overwrite SO_SNDBUF value */
@@ -112,6 +140,28 @@ int _setsockopt(struct bpf_sockopt *ctx)
112140
return 1;
113141
}
114142

143+
if (ctx->level == SOL_IP && ctx->optname == IP_FREEBIND) {
144+
/* Original optlen is larger than PAGE_SIZE. */
145+
if (ctx->optlen != PAGE_SIZE * 2)
146+
return 0; /* EPERM, unexpected data size */
147+
148+
if (optval + 1 > optval_end)
149+
return 0; /* EPERM, bounds check */
150+
151+
/* Make sure we can trim the buffer. */
152+
optval[0] = 0;
153+
ctx->optlen = 1;
154+
155+
/* Usepace buffer is PAGE_SIZE * 2, but BPF
156+
* program can only see the first PAGE_SIZE
157+
* bytes of data.
158+
*/
159+
if (optval_end - optval != PAGE_SIZE)
160+
return 0; /* EPERM, unexpected data size */
161+
162+
return 1;
163+
}
164+
115165
if (ctx->level != SOL_CUSTOM)
116166
return 0; /* EPERM, deny everything except custom level */
117167

0 commit comments

Comments
 (0)