Skip to content

Commit 1b4ae19

Browse files
committed
Daniel Borkmann says: ==================== pull-request: bpf 2023-03-23 We've added 8 non-merge commits during the last 13 day(s) which contain a total of 21 files changed, 238 insertions(+), 161 deletions(-). The main changes are: 1) Fix verification issues in some BPF programs due to their stack usage patterns, from Eduard Zingerman. 2) Fix to add missing overflow checks in xdp_umem_reg and return an error in such case, from Kal Conley. 3) Fix and undo poisoning of strlcpy in libbpf given it broke builds for libcs which provided the former like uClibc-ng, from Jesus Sanchez-Palencia. 4) Fix insufficient bpf_jit_limit default to avoid users running into hard to debug seccomp BPF errors, from Daniel Borkmann. 5) Fix driver return code when they don't support a bpf_xdp_metadata kfunc to make it unambiguous from other errors, from Jesper Dangaard Brouer. 6) Two BPF selftest fixes to address compilation errors from recent changes in kernel structures, from Alexei Starovoitov. * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: xdp: bpf_xdp_metadata use EOPNOTSUPP for no driver support bpf: Adjust insufficient default bpf_jit_limit xsk: Add missing overflow check in xdp_umem_reg selftests/bpf: Fix progs/test_deny_namespace.c issues. selftests/bpf: Fix progs/find_vma_fail1.c build error. libbpf: Revert poisoning of strlcpy selftests/bpf: Tests for uninitialized stack reads bpf: Allow reads from uninit stack ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 2e63a2d + 915efd8 commit 1b4ae19

File tree

21 files changed

+238
-161
lines changed

21 files changed

+238
-161
lines changed

Documentation/networking/xdp-rx-metadata.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ metadata is supported, this set will grow:
2323
An XDP program can use these kfuncs to read the metadata into stack
2424
variables for its own consumption. Or, to pass the metadata on to other
2525
consumers, an XDP program can store it into the metadata area carried
26-
ahead of the packet.
26+
ahead of the packet. Not all packets will necessary have the requested
27+
metadata available in which case the driver returns ``-ENODATA``.
2728

2829
Not all kfuncs have to be implemented by the device driver; when not
29-
implemented, the default ones that return ``-EOPNOTSUPP`` will be used.
30+
implemented, the default ones that return ``-EOPNOTSUPP`` will be used
31+
to indicate the device driver have not implemented this kfunc.
32+
3033

3134
Within an XDP frame, the metadata layout (accessed via ``xdp_buff``) is
3235
as follows::

drivers/net/ethernet/mellanox/mlx4/en_rx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ int mlx4_en_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
674674
struct mlx4_en_xdp_buff *_ctx = (void *)ctx;
675675

676676
if (unlikely(_ctx->ring->hwtstamp_rx_filter != HWTSTAMP_FILTER_ALL))
677-
return -EOPNOTSUPP;
677+
return -ENODATA;
678678

679679
*timestamp = mlx4_en_get_hwtstamp(_ctx->mdev,
680680
mlx4_en_get_cqe_ts(_ctx->cqe));
@@ -686,7 +686,7 @@ int mlx4_en_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash)
686686
struct mlx4_en_xdp_buff *_ctx = (void *)ctx;
687687

688688
if (unlikely(!(_ctx->dev->features & NETIF_F_RXHASH)))
689-
return -EOPNOTSUPP;
689+
return -ENODATA;
690690

691691
*hash = be32_to_cpu(_ctx->cqe->immed_rss_invalid);
692692
return 0;

drivers/net/ethernet/mellanox/mlx5/core/en/xdp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static int mlx5e_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
162162
const struct mlx5e_xdp_buff *_ctx = (void *)ctx;
163163

164164
if (unlikely(!mlx5e_rx_hw_stamp(_ctx->rq->tstamp)))
165-
return -EOPNOTSUPP;
165+
return -ENODATA;
166166

167167
*timestamp = mlx5e_cqe_ts_to_ns(_ctx->rq->ptp_cyc2time,
168168
_ctx->rq->clock, get_cqe_ts(_ctx->cqe));
@@ -174,7 +174,7 @@ static int mlx5e_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash)
174174
const struct mlx5e_xdp_buff *_ctx = (void *)ctx;
175175

176176
if (unlikely(!(_ctx->xdp.rxq->dev->features & NETIF_F_RXHASH)))
177-
return -EOPNOTSUPP;
177+
return -ENODATA;
178178

179179
*hash = be32_to_cpu(_ctx->cqe->rss_hash_result);
180180
return 0;

drivers/net/veth.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,7 @@ static int veth_xdp_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
16421642
struct veth_xdp_buff *_ctx = (void *)ctx;
16431643

16441644
if (!_ctx->skb)
1645-
return -EOPNOTSUPP;
1645+
return -ENODATA;
16461646

16471647
*timestamp = skb_hwtstamps(_ctx->skb)->hwtstamp;
16481648
return 0;
@@ -1653,7 +1653,7 @@ static int veth_xdp_rx_hash(const struct xdp_md *ctx, u32 *hash)
16531653
struct veth_xdp_buff *_ctx = (void *)ctx;
16541654

16551655
if (!_ctx->skb)
1656-
return -EOPNOTSUPP;
1656+
return -ENODATA;
16571657

16581658
*hash = skb_get_hash(_ctx->skb);
16591659
return 0;

kernel/bpf/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ static int __init bpf_jit_charge_init(void)
972972
{
973973
/* Only used as heuristic here to derive limit. */
974974
bpf_jit_limit_max = bpf_jit_alloc_exec_limit();
975-
bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 2,
975+
bpf_jit_limit = min_t(u64, round_up(bpf_jit_limit_max >> 1,
976976
PAGE_SIZE), LONG_MAX);
977977
return 0;
978978
}

kernel/bpf/verifier.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3826,6 +3826,8 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
38263826
continue;
38273827
if (type == STACK_MISC)
38283828
continue;
3829+
if (type == STACK_INVALID && env->allow_uninit_stack)
3830+
continue;
38293831
verbose(env, "invalid read from stack off %d+%d size %d\n",
38303832
off, i, size);
38313833
return -EACCES;
@@ -3863,6 +3865,8 @@ static int check_stack_read_fixed_off(struct bpf_verifier_env *env,
38633865
continue;
38643866
if (type == STACK_ZERO)
38653867
continue;
3868+
if (type == STACK_INVALID && env->allow_uninit_stack)
3869+
continue;
38663870
verbose(env, "invalid read from stack off %d+%d size %d\n",
38673871
off, i, size);
38683872
return -EACCES;
@@ -5754,7 +5758,8 @@ static int check_stack_range_initialized(
57545758
stype = &state->stack[spi].slot_type[slot % BPF_REG_SIZE];
57555759
if (*stype == STACK_MISC)
57565760
goto mark;
5757-
if (*stype == STACK_ZERO) {
5761+
if ((*stype == STACK_ZERO) ||
5762+
(*stype == STACK_INVALID && env->allow_uninit_stack)) {
57585763
if (clobber) {
57595764
/* helper can write anything into the stack */
57605765
*stype = STACK_MISC;
@@ -13936,6 +13941,10 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
1393613941
if (old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_INVALID)
1393713942
continue;
1393813943

13944+
if (env->allow_uninit_stack &&
13945+
old->stack[spi].slot_type[i % BPF_REG_SIZE] == STACK_MISC)
13946+
continue;
13947+
1393913948
/* explored stack has more populated slots than current stack
1394013949
* and these slots were used
1394113950
*/

net/core/xdp.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,10 @@ __diag_ignore_all("-Wmissing-prototypes",
720720
* @ctx: XDP context pointer.
721721
* @timestamp: Return value pointer.
722722
*
723-
* Returns 0 on success or ``-errno`` on error.
723+
* Return:
724+
* * Returns 0 on success or ``-errno`` on error.
725+
* * ``-EOPNOTSUPP`` : means device driver does not implement kfunc
726+
* * ``-ENODATA`` : means no RX-timestamp available for this frame
724727
*/
725728
__bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx, u64 *timestamp)
726729
{
@@ -732,7 +735,10 @@ __bpf_kfunc int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx, u64 *tim
732735
* @ctx: XDP context pointer.
733736
* @hash: Return value pointer.
734737
*
735-
* Returns 0 on success or ``-errno`` on error.
738+
* Return:
739+
* * Returns 0 on success or ``-errno`` on error.
740+
* * ``-EOPNOTSUPP`` : means device driver doesn't implement kfunc
741+
* * ``-ENODATA`` : means no RX-hash available for this frame
736742
*/
737743
__bpf_kfunc int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, u32 *hash)
738744
{

net/xdp/xdp_umem.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
150150

151151
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
152152
{
153-
u32 npgs_rem, chunk_size = mr->chunk_size, headroom = mr->headroom;
154153
bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
155-
u64 npgs, addr = mr->addr, size = mr->len;
156-
unsigned int chunks, chunks_rem;
154+
u32 chunk_size = mr->chunk_size, headroom = mr->headroom;
155+
u64 addr = mr->addr, size = mr->len;
156+
u32 chunks_rem, npgs_rem;
157+
u64 chunks, npgs;
157158
int err;
158159

159160
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) {
@@ -188,8 +189,8 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
188189
if (npgs > U32_MAX)
189190
return -EINVAL;
190191

191-
chunks = (unsigned int)div_u64_rem(size, chunk_size, &chunks_rem);
192-
if (chunks == 0)
192+
chunks = div_u64_rem(size, chunk_size, &chunks_rem);
193+
if (!chunks || chunks > U32_MAX)
193194
return -EINVAL;
194195

195196
if (!unaligned_chunks && chunks_rem)
@@ -202,7 +203,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
202203
umem->headroom = headroom;
203204
umem->chunk_size = chunk_size;
204205
umem->chunks = chunks;
205-
umem->npgs = (u32)npgs;
206+
umem->npgs = npgs;
206207
umem->pgs = NULL;
207208
umem->user = NULL;
208209
umem->flags = mr->flags;

tools/lib/bpf/libbpf_internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
/* make sure libbpf doesn't use kernel-only integer typedefs */
2121
#pragma GCC poison u8 u16 u32 u64 s8 s16 s32 s64
2222

23-
/* prevent accidental re-addition of reallocarray()/strlcpy() */
24-
#pragma GCC poison reallocarray strlcpy
23+
/* prevent accidental re-addition of reallocarray() */
24+
#pragma GCC poison reallocarray
2525

2626
#include "libbpf.h"
2727
#include "btf.h"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "uninit_stack.skel.h"
5+
6+
void test_uninit_stack(void)
7+
{
8+
RUN_TESTS(uninit_stack);
9+
}

0 commit comments

Comments
 (0)