Skip to content

Commit fbde105

Browse files
committed
Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Pull bpf fixes from Alexei Starovoitov: - Finish constification of 1st parameter of bpf_d_path() (Rong Tao) - Harden userspace-supplied xdp_desc validation (Alexander Lobakin) - Fix metadata_dst leak in __bpf_redirect_neigh_v{4,6}() (Daniel Borkmann) - Fix undefined behavior in {get,put}_unaligned_be32() (Eric Biggers) - Use correct context to unpin bpf hash map with special types (KaFai Wan) * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: selftests/bpf: Add test for unpinning htab with internal timer struct bpf: Avoid RCU context warning when unpinning htab with internal structs xsk: Harden userspace-supplied xdp_desc validation bpf: Fix metadata_dst leak __bpf_redirect_neigh_v{4,6} libbpf: Fix undefined behavior in {get,put}_unaligned_be32() bpf: Finish constification of 1st parameter of bpf_d_path()
2 parents ae13bd2 + ffce84b commit fbde105

File tree

10 files changed

+118
-25
lines changed

10 files changed

+118
-25
lines changed

include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4891,7 +4891,7 @@ union bpf_attr {
48914891
*
48924892
* **-ENOENT** if the bpf_local_storage cannot be found.
48934893
*
4894-
* long bpf_d_path(struct path *path, char *buf, u32 sz)
4894+
* long bpf_d_path(const struct path *path, char *buf, u32 sz)
48954895
* Description
48964896
* Return full path for given **struct path** object, which
48974897
* needs to be the kernel BTF *path* object. The path is

kernel/bpf/inode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ static int bpf_show_options(struct seq_file *m, struct dentry *root)
775775
return 0;
776776
}
777777

778-
static void bpf_free_inode(struct inode *inode)
778+
static void bpf_destroy_inode(struct inode *inode)
779779
{
780780
enum bpf_type type;
781781

@@ -790,7 +790,7 @@ const struct super_operations bpf_super_ops = {
790790
.statfs = simple_statfs,
791791
.drop_inode = inode_just_drop,
792792
.show_options = bpf_show_options,
793-
.free_inode = bpf_free_inode,
793+
.destroy_inode = bpf_destroy_inode,
794794
};
795795

796796
enum {

net/core/filter.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,6 +2281,7 @@ static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
22812281
if (IS_ERR(dst))
22822282
goto out_drop;
22832283

2284+
skb_dst_drop(skb);
22842285
skb_dst_set(skb, dst);
22852286
} else if (nh->nh_family != AF_INET6) {
22862287
goto out_drop;
@@ -2389,6 +2390,7 @@ static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
23892390
goto out_drop;
23902391
}
23912392

2393+
skb_dst_drop(skb);
23922394
skb_dst_set(skb, &rt->dst);
23932395
}
23942396

net/xdp/xsk_queue.h

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,42 +143,67 @@ static inline bool xp_unused_options_set(u32 options)
143143
static inline bool xp_aligned_validate_desc(struct xsk_buff_pool *pool,
144144
struct xdp_desc *desc)
145145
{
146-
u64 addr = desc->addr - pool->tx_metadata_len;
147-
u64 len = desc->len + pool->tx_metadata_len;
148-
u64 offset = addr & (pool->chunk_size - 1);
146+
u64 len = desc->len;
147+
u64 addr, offset;
149148

150-
if (!desc->len)
149+
if (!len)
151150
return false;
152151

153-
if (offset + len > pool->chunk_size)
152+
/* Can overflow if desc->addr < pool->tx_metadata_len */
153+
if (check_sub_overflow(desc->addr, pool->tx_metadata_len, &addr))
154+
return false;
155+
156+
offset = addr & (pool->chunk_size - 1);
157+
158+
/*
159+
* Can't overflow: @offset is guaranteed to be < ``U32_MAX``
160+
* (pool->chunk_size is ``u32``), @len is guaranteed
161+
* to be <= ``U32_MAX``.
162+
*/
163+
if (offset + len + pool->tx_metadata_len > pool->chunk_size)
154164
return false;
155165

156166
if (addr >= pool->addrs_cnt)
157167
return false;
158168

159169
if (xp_unused_options_set(desc->options))
160170
return false;
171+
161172
return true;
162173
}
163174

164175
static inline bool xp_unaligned_validate_desc(struct xsk_buff_pool *pool,
165176
struct xdp_desc *desc)
166177
{
167-
u64 addr = xp_unaligned_add_offset_to_addr(desc->addr) - pool->tx_metadata_len;
168-
u64 len = desc->len + pool->tx_metadata_len;
178+
u64 len = desc->len;
179+
u64 addr, end;
169180

170-
if (!desc->len)
181+
if (!len)
171182
return false;
172183

184+
/* Can't overflow: @len is guaranteed to be <= ``U32_MAX`` */
185+
len += pool->tx_metadata_len;
173186
if (len > pool->chunk_size)
174187
return false;
175188

176-
if (addr >= pool->addrs_cnt || addr + len > pool->addrs_cnt ||
177-
xp_desc_crosses_non_contig_pg(pool, addr, len))
189+
/* Can overflow if desc->addr is close to 0 */
190+
if (check_sub_overflow(xp_unaligned_add_offset_to_addr(desc->addr),
191+
pool->tx_metadata_len, &addr))
192+
return false;
193+
194+
if (addr >= pool->addrs_cnt)
195+
return false;
196+
197+
/* Can overflow if pool->addrs_cnt is high enough */
198+
if (check_add_overflow(addr, len, &end) || end > pool->addrs_cnt)
199+
return false;
200+
201+
if (xp_desc_crosses_non_contig_pg(pool, addr, len))
178202
return false;
179203

180204
if (xp_unused_options_set(desc->options))
181205
return false;
206+
182207
return true;
183208
}
184209

scripts/bpf_doc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,7 @@ def __init__(self, parser):
788788
'struct task_struct',
789789
'struct cgroup',
790790
'struct path',
791+
'const struct path',
791792
'struct btf_ptr',
792793
'struct inode',
793794
'struct socket',

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4891,7 +4891,7 @@ union bpf_attr {
48914891
*
48924892
* **-ENOENT** if the bpf_local_storage cannot be found.
48934893
*
4894-
* long bpf_d_path(struct path *path, char *buf, u32 sz)
4894+
* long bpf_d_path(const struct path *path, char *buf, u32 sz)
48954895
* Description
48964896
* Return full path for given **struct path** object, which
48974897
* needs to be the kernel BTF *path* object. The path is

tools/lib/bpf/libbpf_utils.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,20 @@ const char *libbpf_errstr(int err)
148148
}
149149
}
150150

151-
#pragma GCC diagnostic push
152-
#pragma GCC diagnostic ignored "-Wpacked"
153-
#pragma GCC diagnostic ignored "-Wattributes"
154-
struct __packed_u32 { __u32 __val; } __attribute__((packed));
155-
#pragma GCC diagnostic pop
156-
157-
#define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val))
158-
#define put_unaligned_be32(v, p) do { \
159-
((struct __packed_u32 *)(p))->__val = cpu_to_be32(v); \
160-
} while (0)
151+
static inline __u32 get_unaligned_be32(const void *p)
152+
{
153+
__be32 val;
154+
155+
memcpy(&val, p, sizeof(val));
156+
return be32_to_cpu(val);
157+
}
158+
159+
static inline void put_unaligned_be32(__u32 val, void *p)
160+
{
161+
__be32 be_val = cpu_to_be32(val);
162+
163+
memcpy(p, &be_val, sizeof(be_val));
164+
}
161165

162166
#define SHA256_BLOCK_LENGTH 64
163167
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <test_progs.h>
4+
#include "test_pinning_htab.skel.h"
5+
6+
static void unpin_map(const char *map_name, const char *pin_path)
7+
{
8+
struct test_pinning_htab *skel;
9+
struct bpf_map *map;
10+
int err;
11+
12+
skel = test_pinning_htab__open_and_load();
13+
if (!ASSERT_OK_PTR(skel, "skel open_and_load"))
14+
return;
15+
16+
map = bpf_object__find_map_by_name(skel->obj, map_name);
17+
if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
18+
goto out;
19+
20+
err = bpf_map__pin(map, pin_path);
21+
if (!ASSERT_OK(err, "bpf_map__pin"))
22+
goto out;
23+
24+
err = bpf_map__unpin(map, pin_path);
25+
ASSERT_OK(err, "bpf_map__unpin");
26+
out:
27+
test_pinning_htab__destroy(skel);
28+
}
29+
30+
void test_pinning_htab(void)
31+
{
32+
if (test__start_subtest("timer_prealloc"))
33+
unpin_map("timer_prealloc", "/sys/fs/bpf/timer_prealloc");
34+
if (test__start_subtest("timer_no_prealloc"))
35+
unpin_map("timer_no_prealloc", "/sys/fs/bpf/timer_no_prealloc");
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
char _license[] SEC("license") = "GPL";
7+
8+
struct timer_val {
9+
struct bpf_timer timer;
10+
};
11+
12+
struct {
13+
__uint(type, BPF_MAP_TYPE_HASH);
14+
__type(key, __u32);
15+
__type(value, struct timer_val);
16+
__uint(max_entries, 1);
17+
} timer_prealloc SEC(".maps");
18+
19+
struct {
20+
__uint(type, BPF_MAP_TYPE_HASH);
21+
__type(key, __u32);
22+
__type(value, struct timer_val);
23+
__uint(max_entries, 1);
24+
__uint(map_flags, BPF_F_NO_PREALLOC);
25+
} timer_no_prealloc SEC(".maps");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ __success
7070
int BPF_PROG(path_d_path_from_file_argument, struct file *file)
7171
{
7272
int ret;
73-
struct path *path;
73+
const struct path *path;
7474

7575
/* The f_path member is a path which is embedded directly within a
7676
* file. Therefore, a pointer to such embedded members are still

0 commit comments

Comments
 (0)