Skip to content

Commit 0517fa9

Browse files
committed
Merge: bpf: stable backports from 6.14
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/7050 Backport of selected patches from 6.14 (mostly taken from stable 6.12 branch since BPF on RHEL 9 has been rebased to 6.12). JIRA: https://issues.redhat.com/browse/RHEL-85486 JIRA: https://issues.redhat.com/browse/RHEL-96489 JIRA: https://issues.redhat.com/browse/RHEL-81243 JIRA: https://issues.redhat.com/browse/RHEL-83203 JIRA: https://issues.redhat.com/browse/RHEL-83308 JIRA: https://issues.redhat.com/browse/RHEL-83348 CVE: CVE-2025-21728 CVE: CVE-2025-21851 CVE: CVE-2024-58088 CVE: CVE-2025-21853 Signed-off-by: Jerome Marchand <jmarchan@redhat.com> Approved-by: Viktor Malik <vmalik@redhat.com> Approved-by: Gregory Bell <grbell@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Augusto Caringi <acaringi@redhat.com>
2 parents bbeb51b + 9ee2336 commit 0517fa9

File tree

23 files changed

+460
-83
lines changed

23 files changed

+460
-83
lines changed

include/linux/btf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ static inline bool btf_type_is_scalar(const struct btf_type *t)
351351
return btf_type_is_int(t) || btf_type_is_enum(t);
352352
}
353353

354+
static inline bool btf_type_is_fwd(const struct btf_type *t)
355+
{
356+
return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
357+
}
358+
354359
static inline bool btf_type_is_typedef(const struct btf_type *t)
355360
{
356361
return BTF_INFO_KIND(t->info) == BTF_KIND_TYPEDEF;

kernel/bpf/arena.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*/
3939

4040
/* number of bytes addressable by LDX/STX insn with 16-bit 'off' field */
41-
#define GUARD_SZ (1ull << sizeof_field(struct bpf_insn, off) * 8)
41+
#define GUARD_SZ round_up(1ull << sizeof_field(struct bpf_insn, off) * 8, PAGE_SIZE << 1)
4242
#define KERN_VM_SZ (SZ_4G + GUARD_SZ)
4343

4444
struct bpf_arena {
@@ -216,7 +216,7 @@ static u64 arena_map_mem_usage(const struct bpf_map *map)
216216
struct vma_list {
217217
struct vm_area_struct *vma;
218218
struct list_head head;
219-
atomic_t mmap_count;
219+
refcount_t mmap_count;
220220
};
221221

222222
static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
@@ -226,7 +226,7 @@ static int remember_vma(struct bpf_arena *arena, struct vm_area_struct *vma)
226226
vml = kmalloc(sizeof(*vml), GFP_KERNEL);
227227
if (!vml)
228228
return -ENOMEM;
229-
atomic_set(&vml->mmap_count, 1);
229+
refcount_set(&vml->mmap_count, 1);
230230
vma->vm_private_data = vml;
231231
vml->vma = vma;
232232
list_add(&vml->head, &arena->vma_list);
@@ -237,7 +237,7 @@ static void arena_vm_open(struct vm_area_struct *vma)
237237
{
238238
struct vma_list *vml = vma->vm_private_data;
239239

240-
atomic_inc(&vml->mmap_count);
240+
refcount_inc(&vml->mmap_count);
241241
}
242242

243243
static void arena_vm_close(struct vm_area_struct *vma)
@@ -246,7 +246,7 @@ static void arena_vm_close(struct vm_area_struct *vma)
246246
struct bpf_arena *arena = container_of(map, struct bpf_arena, map);
247247
struct vma_list *vml = vma->vm_private_data;
248248

249-
if (!atomic_dec_and_test(&vml->mmap_count))
249+
if (!refcount_dec_and_test(&vml->mmap_count))
250250
return;
251251
guard(mutex)(&arena->lock);
252252
/* update link list under lock */

kernel/bpf/bpf_cgrp_storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr)
154154

155155
static void cgroup_storage_map_free(struct bpf_map *map)
156156
{
157-
bpf_local_storage_map_free(map, &cgroup_cache, NULL);
157+
bpf_local_storage_map_free(map, &cgroup_cache, &bpf_cgrp_storage_busy);
158158
}
159159

160160
/* *gfp_flags* is a hidden argument provided by the verifier */

kernel/bpf/bpf_local_storage.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,8 +797,12 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,
797797
smap->elem_size = offsetof(struct bpf_local_storage_elem,
798798
sdata.data[attr->value_size]);
799799

800-
smap->bpf_ma = bpf_ma;
801-
if (bpf_ma) {
800+
/* In PREEMPT_RT, kmalloc(GFP_ATOMIC) is still not safe in non
801+
* preemptible context. Thus, enforce all storages to use
802+
* bpf_mem_alloc when CONFIG_PREEMPT_RT is enabled.
803+
*/
804+
smap->bpf_ma = IS_ENABLED(CONFIG_PREEMPT_RT) ? true : bpf_ma;
805+
if (smap->bpf_ma) {
802806
err = bpf_mem_alloc_init(&smap->selem_ma, smap->elem_size, false);
803807
if (err)
804808
goto free_smap;

kernel/bpf/bpf_struct_ops.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,20 @@ void bpf_struct_ops_desc_release(struct bpf_struct_ops_desc *st_ops_desc)
311311
kfree(arg_info);
312312
}
313313

314+
static bool is_module_member(const struct btf *btf, u32 id)
315+
{
316+
const struct btf_type *t;
317+
318+
t = btf_type_resolve_ptr(btf, id, NULL);
319+
if (!t)
320+
return false;
321+
322+
if (!__btf_type_is_struct(t) && !btf_type_is_fwd(t))
323+
return false;
324+
325+
return !strcmp(btf_name_by_offset(btf, t->name_off), "module");
326+
}
327+
314328
int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
315329
struct btf *btf,
316330
struct bpf_verifier_log *log)
@@ -390,6 +404,13 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
390404
goto errout;
391405
}
392406

407+
if (!st_ops_ids[IDX_MODULE_ID] && is_module_member(btf, member->type)) {
408+
pr_warn("'struct module' btf id not found. Is CONFIG_MODULES enabled? bpf_struct_ops '%s' needs module support.\n",
409+
st_ops->name);
410+
err = -EOPNOTSUPP;
411+
goto errout;
412+
}
413+
393414
func_proto = btf_type_resolve_func_ptr(btf,
394415
member->type,
395416
NULL);

kernel/bpf/btf.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,6 @@ bool btf_type_is_void(const struct btf_type *t)
497497
return t == &btf_void;
498498
}
499499

500-
static bool btf_type_is_fwd(const struct btf_type *t)
501-
{
502-
return BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
503-
}
504-
505500
static bool btf_type_is_datasec(const struct btf_type *t)
506501
{
507502
return BTF_INFO_KIND(t->info) == BTF_KIND_DATASEC;

kernel/bpf/helpers.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,10 +1593,24 @@ void bpf_timer_cancel_and_free(void *val)
15931593
* To avoid these issues, punt to workqueue context when we are in a
15941594
* timer callback.
15951595
*/
1596-
if (this_cpu_read(hrtimer_running))
1596+
if (this_cpu_read(hrtimer_running)) {
15971597
queue_work(system_unbound_wq, &t->cb.delete_work);
1598-
else
1598+
return;
1599+
}
1600+
1601+
if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
1602+
/* If the timer is running on other CPU, also use a kworker to
1603+
* wait for the completion of the timer instead of trying to
1604+
* acquire a sleepable lock in hrtimer_cancel() to wait for its
1605+
* completion.
1606+
*/
1607+
if (hrtimer_try_to_cancel(&t->timer) >= 0)
1608+
kfree_rcu(t, cb.rcu);
1609+
else
1610+
queue_work(system_unbound_wq, &t->cb.delete_work);
1611+
} else {
15991612
bpf_timer_delete_work(&t->cb.delete_work);
1613+
}
16001614
}
16011615

16021616
/* This function is called by map_delete/update_elem for individual element and

kernel/bpf/ringbuf.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ static int ringbuf_map_mmap_kern(struct bpf_map *map, struct vm_area_struct *vma
268268
/* allow writable mapping for the consumer_pos only */
269269
if (vma->vm_pgoff != 0 || vma->vm_end - vma->vm_start != PAGE_SIZE)
270270
return -EPERM;
271-
} else {
272-
vm_flags_clear(vma, VM_MAYWRITE);
273271
}
274272
/* remap_vmalloc_range() checks size and offset constraints */
275273
return remap_vmalloc_range(vma, rb_map->rb,
@@ -289,8 +287,6 @@ static int ringbuf_map_mmap_user(struct bpf_map *map, struct vm_area_struct *vma
289287
* position, and the ring buffer data itself.
290288
*/
291289
return -EPERM;
292-
} else {
293-
vm_flags_clear(vma, VM_MAYWRITE);
294290
}
295291
/* remap_vmalloc_range() checks size and offset constraints */
296292
return remap_vmalloc_range(vma, rb_map->rb, vma->vm_pgoff + RINGBUF_PGOFF);

kernel/bpf/syscall.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ static const struct vm_operations_struct bpf_map_default_vmops = {
953953
static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
954954
{
955955
struct bpf_map *map = filp->private_data;
956-
int err;
956+
int err = 0;
957957

958958
if (!map->ops->map_mmap || !IS_ERR_OR_NULL(map->record))
959959
return -ENOTSUPP;
@@ -977,24 +977,33 @@ static int bpf_map_mmap(struct file *filp, struct vm_area_struct *vma)
977977
err = -EACCES;
978978
goto out;
979979
}
980+
bpf_map_write_active_inc(map);
980981
}
982+
out:
983+
mutex_unlock(&map->freeze_mutex);
984+
if (err)
985+
return err;
981986

982987
/* set default open/close callbacks */
983988
vma->vm_ops = &bpf_map_default_vmops;
984989
vma->vm_private_data = map;
985990
vm_flags_clear(vma, VM_MAYEXEC);
991+
/* If mapping is read-only, then disallow potentially re-mapping with
992+
* PROT_WRITE by dropping VM_MAYWRITE flag. This VM_MAYWRITE clearing
993+
* means that as far as BPF map's memory-mapped VMAs are concerned,
994+
* VM_WRITE and VM_MAYWRITE and equivalent, if one of them is set,
995+
* both should be set, so we can forget about VM_MAYWRITE and always
996+
* check just VM_WRITE
997+
*/
986998
if (!(vma->vm_flags & VM_WRITE))
987-
/* disallow re-mapping with PROT_WRITE */
988999
vm_flags_clear(vma, VM_MAYWRITE);
9891000

9901001
err = map->ops->map_mmap(map, vma);
991-
if (err)
992-
goto out;
1002+
if (err) {
1003+
if (vma->vm_flags & VM_WRITE)
1004+
bpf_map_write_active_dec(map);
1005+
}
9931006

994-
if (vma->vm_flags & VM_MAYWRITE)
995-
bpf_map_write_active_inc(map);
996-
out:
997-
mutex_unlock(&map->freeze_mutex);
9981007
return err;
9991008
}
10001009

@@ -1927,8 +1936,6 @@ int generic_map_update_batch(struct bpf_map *map, struct file *map_file,
19271936
return err;
19281937
}
19291938

1930-
#define MAP_LOOKUP_RETRIES 3
1931-
19321939
int generic_map_lookup_batch(struct bpf_map *map,
19331940
const union bpf_attr *attr,
19341941
union bpf_attr __user *uattr)
@@ -1938,8 +1945,8 @@ int generic_map_lookup_batch(struct bpf_map *map,
19381945
void __user *values = u64_to_user_ptr(attr->batch.values);
19391946
void __user *keys = u64_to_user_ptr(attr->batch.keys);
19401947
void *buf, *buf_prevkey, *prev_key, *key, *value;
1941-
int err, retry = MAP_LOOKUP_RETRIES;
19421948
u32 value_size, cp, max_count;
1949+
int err;
19431950

19441951
if (attr->batch.elem_flags & ~BPF_F_LOCK)
19451952
return -EINVAL;
@@ -1985,14 +1992,8 @@ int generic_map_lookup_batch(struct bpf_map *map,
19851992
err = bpf_map_copy_value(map, key, value,
19861993
attr->batch.elem_flags);
19871994

1988-
if (err == -ENOENT) {
1989-
if (retry) {
1990-
retry--;
1991-
continue;
1992-
}
1993-
err = -EINTR;
1994-
break;
1995-
}
1995+
if (err == -ENOENT)
1996+
goto next_key;
19961997

19971998
if (err)
19981999
goto free_buf;
@@ -2007,12 +2008,12 @@ int generic_map_lookup_batch(struct bpf_map *map,
20072008
goto free_buf;
20082009
}
20092010

2011+
cp++;
2012+
next_key:
20102013
if (!prev_key)
20112014
prev_key = buf_prevkey;
20122015

20132016
swap(prev_key, key);
2014-
retry = MAP_LOOKUP_RETRIES;
2015-
cp++;
20162017
cond_resched();
20172018
}
20182019

kernel/trace/bpf_trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ static int bpf_send_signal_common(u32 sig, enum pid_type type)
832832
if (unlikely(is_global_init(current)))
833833
return -EPERM;
834834

835-
if (irqs_disabled()) {
835+
if (preempt_count() != 0 || irqs_disabled()) {
836836
/* Do an early check on signal validity. Otherwise,
837837
* the error is lost in deferred irq_work.
838838
*/

0 commit comments

Comments
 (0)