Skip to content

Commit 833d67e

Browse files
fomichevborkmann
authored andcommitted
selftests/bpf: Verify optval=NULL case
Make sure we get optlen exported instead of getting EFAULT. Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 00e74ae commit 833d67e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cgroup_helpers.h"
44

55
#include <linux/tcp.h>
6+
#include <linux/netlink.h>
67
#include "sockopt_sk.skel.h"
78

89
#ifndef SOL_TCP
@@ -183,6 +184,33 @@ static int getsetsockopt(void)
183184
goto err;
184185
}
185186

187+
/* optval=NULL case is handled correctly */
188+
189+
close(fd);
190+
fd = socket(AF_NETLINK, SOCK_RAW, 0);
191+
if (fd < 0) {
192+
log_err("Failed to create AF_NETLINK socket");
193+
return -1;
194+
}
195+
196+
buf.u32 = 1;
197+
optlen = sizeof(__u32);
198+
err = setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &buf, optlen);
199+
if (err) {
200+
log_err("Unexpected getsockopt(NETLINK_ADD_MEMBERSHIP) err=%d errno=%d",
201+
err, errno);
202+
goto err;
203+
}
204+
205+
optlen = 0;
206+
err = getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &optlen);
207+
if (err) {
208+
log_err("Unexpected getsockopt(NETLINK_LIST_MEMBERSHIPS) err=%d errno=%d",
209+
err, errno);
210+
goto err;
211+
}
212+
ASSERT_EQ(optlen, 4, "Unexpected NETLINK_LIST_MEMBERSHIPS value");
213+
186214
free(big_buf);
187215
close(fd);
188216
return 0;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ int _getsockopt(struct bpf_sockopt *ctx)
3232
__u8 *optval_end = ctx->optval_end;
3333
__u8 *optval = ctx->optval;
3434
struct sockopt_sk *storage;
35+
struct bpf_sock *sk;
36+
37+
/* Bypass AF_NETLINK. */
38+
sk = ctx->sk;
39+
if (sk && sk->family == AF_NETLINK)
40+
return 1;
3541

3642
/* Make sure bpf_get_netns_cookie is callable.
3743
*/
@@ -131,6 +137,12 @@ int _setsockopt(struct bpf_sockopt *ctx)
131137
__u8 *optval_end = ctx->optval_end;
132138
__u8 *optval = ctx->optval;
133139
struct sockopt_sk *storage;
140+
struct bpf_sock *sk;
141+
142+
/* Bypass AF_NETLINK. */
143+
sk = ctx->sk;
144+
if (sk && sk->family == AF_NETLINK)
145+
return 1;
134146

135147
/* Make sure bpf_get_netns_cookie is callable.
136148
*/

0 commit comments

Comments
 (0)