bpf: Make bpf_refcount_acquire fallible for non-owning refs
Linux
Affected versions
| Product | Affected | Fixed in |
|---|---|---|
| Linux Linux |
| No fix yet |
| Linux Linux |
|
|
Original description
In the Linux kernel, the following vulnerability has been resolved:
bpf: Make bpf_refcount_acquire fallible for non-owning refs
This patch fixes an incorrect assumption made in the original bpf_refcount series [0], specifically that the BPF program calling bpf_refcount_acquire on some node can always guarantee that the node is alive. In that series, the patch adding failure behavior to rbtree_add and list_push_{front, back} breaks this assumption for non-owning references.
Consider the following program:
n = bpf_kptr_xchg(&mapval, NULL); /* skip error checking */
bpf_spin_lock(&l); if(bpf_rbtree_add(&t, &n->rb, less)) { bpf_refcount_acquire(n); /* Failed to add, do something else with the node */ } bpf_spin_unlock(&l);
It's incorrect to assume that bpf_refcount_acquire will always succeed in this scenario. bpf_refcount_acquire is being called in a critical section here, but the lock being held is associated with rbtree t, which isn't necessarily the lock associated with the tree that the node is already in. So after bpf_rbtree_add fails to add the node and calls bpf_obj_drop in it, the program has no ownership of the node's lifetime. Therefore the node's refcount can be decr'd to 0 at any time after the failing rbtree_add. If this happens before the refcount_acquire above, the node might be free'd, and regardless refcount_acquire will be incrementing a 0 refcount.
Later patches in the series exercise this scenario, resulting in the expected complaint from the kernel (without this patch's changes):
From the CVE record · Linux
References
- git.kernel.org/stable/c/d906d1b940b9dbf0a3e821d6b32a51c369273d91Patch
- git.kernel.org/stable/c/7793fc3babe9fea908e57f7c187ea819f9fd7e95Patch
- github.com/CVEProject/cvelistV5/tree/main/cves/2023/53xxx/CVE-2023-53645.json
- nvd.nist.gov/vuln/detail/CVE-2023-53645
- git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git