GCC's -Wall includes -Wsign-compare while clang does not. Since BPF programs are built with clang we need to add this flag explicitly to catch problematic comparisons like: int i = -1; unsigned int j = 1; if (i < j) // this is false. long i = -1; unsigned int j = 1; if (i < j) // this is true. C standard for reference: - If either operand is unsigned long the other shall be converted to unsigned long. - Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int. - Otherwise, if either operand is long, the other shall be converted to long. - Otherwise, if either operand is unsigned, the other shall be converted to unsigned. Unfortunately clang's -Wsign-compare is very noisy. It complains about (s32)a == (u32)b which is safe and doen't have surprising behavior. This patch fixes some of the issues. It needs a follow up to fix the rest. Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Acked-by: Jiri Olsa <jolsa@kernel.org> Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> Link: https://lore.kernel.org/bpf/20231226191148.48536-2-alexei.starovoitov@gmail.com
90 lines
1.8 KiB
C
90 lines
1.8 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <netinet/in.h>
|
|
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
|
|
char _license[] SEC("license") = "GPL";
|
|
|
|
__s32 page_size = 0;
|
|
|
|
SEC("cgroup/getsockopt")
|
|
int _getsockopt_child(struct bpf_sockopt *ctx)
|
|
{
|
|
__u8 *optval_end = ctx->optval_end;
|
|
__u8 *optval = ctx->optval;
|
|
|
|
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
|
|
goto out;
|
|
|
|
if (optval + 1 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
if (optval[0] != 0x80)
|
|
return 0; /* EPERM, unexpected optval from the kernel */
|
|
|
|
ctx->retval = 0; /* Reset system call return value to zero */
|
|
|
|
optval[0] = 0x90;
|
|
ctx->optlen = 1;
|
|
|
|
return 1;
|
|
|
|
out:
|
|
/* optval larger than PAGE_SIZE use kernel's buffer. */
|
|
if (ctx->optlen > page_size)
|
|
ctx->optlen = 0;
|
|
return 1;
|
|
}
|
|
|
|
SEC("cgroup/getsockopt")
|
|
int _getsockopt_parent(struct bpf_sockopt *ctx)
|
|
{
|
|
__u8 *optval_end = ctx->optval_end;
|
|
__u8 *optval = ctx->optval;
|
|
|
|
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
|
|
goto out;
|
|
|
|
if (optval + 1 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
if (optval[0] != 0x90)
|
|
return 0; /* EPERM, unexpected optval from the kernel */
|
|
|
|
ctx->retval = 0; /* Reset system call return value to zero */
|
|
|
|
optval[0] = 0xA0;
|
|
ctx->optlen = 1;
|
|
|
|
return 1;
|
|
|
|
out:
|
|
/* optval larger than PAGE_SIZE use kernel's buffer. */
|
|
if (ctx->optlen > page_size)
|
|
ctx->optlen = 0;
|
|
return 1;
|
|
}
|
|
|
|
SEC("cgroup/setsockopt")
|
|
int _setsockopt(struct bpf_sockopt *ctx)
|
|
{
|
|
__u8 *optval_end = ctx->optval_end;
|
|
__u8 *optval = ctx->optval;
|
|
|
|
if (ctx->level != SOL_IP || ctx->optname != IP_TOS)
|
|
goto out;
|
|
|
|
if (optval + 1 > optval_end)
|
|
return 0; /* EPERM, bounds check */
|
|
|
|
optval[0] += 0x10;
|
|
ctx->optlen = 1;
|
|
|
|
return 1;
|
|
|
|
out:
|
|
/* optval larger than PAGE_SIZE use kernel's buffer. */
|
|
if (ctx->optlen > page_size)
|
|
ctx->optlen = 0;
|
|
return 1;
|
|
}
|