Skip to content

Commit

Permalink
selftests/bpf: Verify optval=NULL case
Browse files Browse the repository at this point in the history
[ Upstream commit 833d67e ]

Make sure we get optlen exported instead of getting EFAULT.

Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20230418225343.553806-3-sdf@google.com
Stable-dep-of: 69844e3 ("selftests/bpf: Fix sockopt_sk selftest")
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
fomichev authored and gregkh committed Jun 14, 2023
1 parent 1a3ceea commit ae14f7f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/sockopt_sk.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "cgroup_helpers.h"

#include <linux/tcp.h>
#include <linux/netlink.h>
#include "sockopt_sk.skel.h"

#ifndef SOL_TCP
Expand Down Expand Up @@ -183,6 +184,33 @@ static int getsetsockopt(void)
goto err;
}

/* optval=NULL case is handled correctly */

close(fd);
fd = socket(AF_NETLINK, SOCK_RAW, 0);
if (fd < 0) {
log_err("Failed to create AF_NETLINK socket");
return -1;
}

buf.u32 = 1;
optlen = sizeof(__u32);
err = setsockopt(fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP, &buf, optlen);
if (err) {
log_err("Unexpected getsockopt(NETLINK_ADD_MEMBERSHIP) err=%d errno=%d",
err, errno);
goto err;
}

optlen = 0;
err = getsockopt(fd, SOL_NETLINK, NETLINK_LIST_MEMBERSHIPS, NULL, &optlen);
if (err) {
log_err("Unexpected getsockopt(NETLINK_LIST_MEMBERSHIPS) err=%d errno=%d",
err, errno);
goto err;
}
ASSERT_EQ(optlen, 4, "Unexpected NETLINK_LIST_MEMBERSHIPS value");

free(big_buf);
close(fd);
return 0;
Expand Down
12 changes: 12 additions & 0 deletions tools/testing/selftests/bpf/progs/sockopt_sk.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ int _getsockopt(struct bpf_sockopt *ctx)
__u8 *optval_end = ctx->optval_end;
__u8 *optval = ctx->optval;
struct sockopt_sk *storage;
struct bpf_sock *sk;

/* Bypass AF_NETLINK. */
sk = ctx->sk;
if (sk && sk->family == AF_NETLINK)
return 1;

/* Make sure bpf_get_netns_cookie is callable.
*/
Expand Down Expand Up @@ -131,6 +137,12 @@ int _setsockopt(struct bpf_sockopt *ctx)
__u8 *optval_end = ctx->optval_end;
__u8 *optval = ctx->optval;
struct sockopt_sk *storage;
struct bpf_sock *sk;

/* Bypass AF_NETLINK. */
sk = ctx->sk;
if (sk && sk->family == AF_NETLINK)
return 1;

/* Make sure bpf_get_netns_cookie is callable.
*/
Expand Down

0 comments on commit ae14f7f

Please sign in to comment.