bpf: local storage helpers should check nullness of owner ptr passed
PATCH 1/1 bpf: local storage helpers should check nullness of owner ptr passed
The verifier allows ARG_PTR_TO_BTF_ID helper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.
The issue can be reproduced by attaching an LSM program to inode_rename hook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:
mv existing_file new_file_does_not_exist
The report including the sample program and the steps for reproducing the bug:
https://lore.kernel.org/bpf/CANaYP3HWkH91SN=wTNO9FL_2ztHfqcXKX38SSE-JJ2voh+vssw@mail.gmail.com
--- kernel/bpf/bpf_inode_storage.c | 5 ++++- kernel/bpf/bpf_task_storage.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/bpf_inode_storage.c +4 -1
@@ -176,7 +176,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!inode_storage_ptr (inode)) +if (!inode || !inode_storage_ptr (inode))return (unsigned long )NULL ; sdata =inode_storage_lookup (inode, map, true);
@@ -200,6 +200,9 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,BPF_CALL_2 (bpf_inode_storage_delete, struct bpf_map *, map, struct inode *, inode) { +if (!inode) +return -EINVAL ; +/* This helper must only called from where the inode is gurranteed * to have a refcount and cannot be freed. */
kernel/bpf/bpf_task_storage.c +4 -1
@@ -218,7 +218,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!task_storage_ptr (task)) +if (!task || !task_storage_ptr (task))return (unsigned long )NULL ; sdata =task_storage_lookup (task, map, true);
@@ -243,6 +243,9 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, task) { +if (!task) +return -EINVAL ; +/* This helper must only be called from places where the lifetime of the task * is guaranteed. Either by being refcounted or by being protected * by an RCU read-side critical section.
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Quoted 6 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.
Quoted 6 lines
The issue can be reproduced by attaching an LSM program to
inode_renamehook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:mv
existing_filenew_file_does_not_exist
Seems like it's simple to write a selftest for this then?
Quoted 20 lines
The report including the sample program and the steps for reproducing the bug:
https://lore.kernel.org/bpf/CANaYP3HWkH91SN=wTNO9FL_2ztHfqcXKX38SSE-JJ2voh+vssw@mail.gmail.com
Fixes 4cf1bc1f1045 ("bpf: Implement task local storage")Fixes 8ea636848aca ("bpf: Implement bpf_local_storage for inodes")Reported-by Gilad Reti <[email protected]>Signed-off-by KP Singh <[email protected]>--- kernel/bpf/bpf_inode_storage.c | 5 ++++- kernel/bpf/bpf_task_storage.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-)kernel/bpf/bpf_inode_storage.c +0
-0
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 179
Quoted 5 lines
kernel/bpf/bpf_inode_storage.c +1
-1@@ -176,7 +176,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!inode_storage_ptr (inode)) +if (!inode || !inode_storage_ptr (inode))
would it be bad to move !inode check inside inode_storage_ptr itself? same for task_storage_ptr() below.
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 206
Quoted 11 lines
kernel/bpf/bpf_inode_storage.c +3
-0return (unsigned long )NULL ; sdata =inode_storage_lookup (inode, map, true);@@ -200,6 +200,9 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,BPF_CALL_2 (bpf_inode_storage_delete, struct bpf_map *, map, struct inode *, inode) { +if (!inode) +return -EINVAL ; +/* This helper must only called from where the inode is gurranteed
Gmail highlights a typo in "gurranteed" ;)
Quoted 28 lines
kernel/bpf/bpf_inode_storage.c +0
-0* to have a refcount and cannot be freed. */ kernel/bpf/bpf_task_storage.c +4
-2@@ -218,7 +218,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!task_storage_ptr (task)) +if (!task || !task_storage_ptr (task))return (unsigned long )NULL ; sdata =task_storage_lookup (task, map, true);@@ -243,6 +243,9 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, task) { +if (!task) +return -EINVAL ; +/* This helper must only be called from places where the lifetime of the task * is guaranteed. Either by being refcounted or by being protected * by an RCU read-side critical section. -- 2.30.0.284.gd98b1dd5eaa7-goog
Quoted 32 lines · Andrii Nakryiko
Quoted 6 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.Quoted 6 lines
The issue can be reproduced by attaching an LSM program to
inode_renamehook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:mv
existing_filenew_file_does_not_existSeems like it's simple to write a selftest for this then?
Quoted 15 lines
The report including the sample program and the steps for reproducing the bug:
https://lore.kernel.org/bpf/CANaYP3HWkH91SN=wTNO9FL_2ztHfqcXKX38SSE-JJ2voh+vssw@mail.gmail.com
Fixes 4cf1bc1f1045 ("bpf: Implement task local storage")Fixes 8ea636848aca ("bpf: Implement bpf_local_storage for inodes")Reported-by Gilad Reti <[email protected]>Signed-off-by KP Singh <[email protected]>--- kernel/bpf/bpf_inode_storage.c | 5 ++++- kernel/bpf/bpf_task_storage.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-)
Quoted 13 lines
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 179
Quoted 10 lines
kernel/bpf/bpf_inode_storage.c +1
-1@@ -176,7 +176,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!inode_storage_ptr (inode)) +if (!inode || !inode_storage_ptr (inode))would it be bad to move !inode check inside
inode_storage_ptritself? same fortask_storage_ptr()below.
And for deletes, inode_storage_delete calls into inode_storage_lookup(), which also seems like a reasonable place to check for null? Even better, inode_storage_lookup() shares logic with inode_storage_ptr(), so if we make sure that all code calls inode_storage_ptr(), then we need to check for NULL just in inode_storage_ptr().
I totally might be missing some subtleties, of course.
Quoted 43 lines
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 206
Quoted 12 lines
kernel/bpf/bpf_inode_storage.c +3
-0return (unsigned long )NULL ; sdata =inode_storage_lookup (inode, map, true);@@ -200,6 +200,9 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,BPF_CALL_2 (bpf_inode_storage_delete, struct bpf_map *, map, struct inode *, inode) { +if (!inode) +return -EINVAL ; +/* This helper must only called from where the inode is gurranteed Gmail highlights a typo in "gurranteed" ;)
Quoted 28 lines
kernel/bpf/bpf_inode_storage.c +0
-0* to have a refcount and cannot be freed. */ kernel/bpf/bpf_task_storage.c +4
-2@@ -218,7 +218,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!task_storage_ptr (task)) +if (!task || !task_storage_ptr (task))return (unsigned long )NULL ; sdata =task_storage_lookup (task, map, true);@@ -243,6 +243,9 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, task) { +if (!task) +return -EINVAL ; +/* This helper must only be called from places where the lifetime of the task * is guaranteed. Either by being refcounted or by being protected * by an RCU read-side critical section. -- 2.30.0.284.gd98b1dd5eaa7-goog
Quoted 11 lines · Andrii Nakryiko
Quoted 8 lines · Andrii Nakryiko
Quoted 6 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
The issue can be reproduced by attaching an LSM program to
inode_renamehook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:mv
existing_filenew_file_does_not_existSeems like it's simple to write a selftest for this then?
Sure, I will send in a separate patch for selftest and also for the typo.
Quoted 30 lines
Quoted 16 lines
Quoted 16 lines
The report including the sample program and the steps for reproducing the bug:
https://lore.kernel.org/bpf/CANaYP3HWkH91SN=wTNO9FL_2ztHfqcXKX38SSE-JJ2voh+vssw@mail.gmail.com
Fixes 4cf1bc1f1045 ("bpf: Implement task local storage")Fixes 8ea636848aca ("bpf: Implement bpf_local_storage for inodes")Reported-by Gilad Reti <[email protected]>Signed-off-by KP Singh <[email protected]>--- kernel/bpf/bpf_inode_storage.c | 5 ++++- kernel/bpf/bpf_task_storage.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-)Quoted 13 lines
kernel/bpf/bpf_inode_storage.c BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode, · line 179
Quoted 10 lines
kernel/bpf/bpf_inode_storage.c +1
-1@@ -176,7 +176,7 @@ BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!inode_storage_ptr (inode)) +if (!inode || !inode_storage_ptr (inode))would it be bad to move !inode check inside
inode_storage_ptritself? same fortask_storage_ptr()below.
Quoted 8 lines
And for deletes,
inode_storage_deletecalls intoinode_storage_lookup(), which also seems like a reasonable place to check for null? Even better,inode_storage_lookup()shares logic withinode_storage_ptr(), so if we make sure that all code callsinode_storage_ptr(), then we need to check for NULL just ininode_storage_ptr().I totally might be missing some subtleties, of course.
All these are good candidates for nullness checks too (I also thought about bpf_inode andbpf_task having a null check).
I kind of like the explicit check / input validation in the helper before it does anything with the pointer.
It's a reminder that the value cannot be assumed to be NULL.
FWIW, we do a similar explicit check in the socket storage code as well.
[...]
Quoted 2 lines
Quoted 2 lines
Gmail highlights a typo in "gurranteed" ;)
Thanks, and thanks gmail ;)
Quoted 29 lines
Quoted 29 lines
Quoted 29 lines
kernel/bpf/bpf_inode_storage.c +0
-0* to have a refcount and cannot be freed. */ kernel/bpf/bpf_task_storage.c +4
-2@@ -218,7 +218,7 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *,* bpf_local_storage_update expects the owner to have a * valid storage pointer. */ -if (!task_storage_ptr (task)) +if (!task || !task_storage_ptr (task))return (unsigned long )NULL ; sdata =task_storage_lookup (task, map, true);@@ -243,6 +243,9 @@ BPF_CALL_4(bpf_task_storage_get, struct bpf_map *, map, struct task_struct *, BPF_CALL_2(bpf_task_storage_delete, struct bpf_map *, map, struct task_struct *, task) { +if (!task) +return -EINVAL ; +/* This helper must only be called from places where the lifetime of the task * is guaranteed. Either by being refcounted or by being protected * by an RCU read-side critical section. -- 2.30.0.284.gd98b1dd5eaa7-goog
Daniel Borkmann
maintainerQuoted 16 lines · KP Singh
Quoted 14 lines · Andrii Nakryiko
Quoted 12 lines · Andrii Nakryiko
Quoted 11 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.The issue can be reproduced by attaching an LSM program to
inode_renamehook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
mv
existing_filenew_file_does_not_existSeems like it's simple to write a selftest for this then?
Sure, I will send in a separate patch for selftest and also for the typo.
If it's small or trivial to add a selftest for the fix, I'd suggest to add it as part of this series for 'ease of logistics' as it would otherwise be a bit odd to i) either have a stand-alone patch against bpf tree with just a selftest or ii) having to wait until bpf syncs into bpf-next and then send one against bpf-next where for the latter there's risk that it gets forgotten in meantime as it might take a while.
Thanks,
Daniel
Quoted 22 lines · Daniel Borkmann
Quoted 20 lines · KP Singh
Quoted 17 lines · Andrii Nakryiko
Quoted 15 lines · Andrii Nakryiko
Quoted 6 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.Quoted 6 lines
The issue can be reproduced by attaching an LSM program to
inode_renamehook (called when moving files) which tries to get the inode of the new file without checking for its nullness and then trying to move an existing file to a new path:mv
existing_filenew_file_does_not_existSeems like it's simple to write a selftest for this then?
Quoted 8 lines
Quoted 1 line
Sure, I will send in a separate patch for selftest and also for the typo.
If it's small or trivial to add a selftest for the fix, I'd suggest to add it as part of this series for 'ease of logistics' as it would otherwise be a bit odd to i) either have a stand-alone patch against bpf tree with just a selftest or ii) having to wait until bpf syncs into bpf-next and then send one against bpf-next where for the latter there's risk that it gets forgotten in meantime as it might take a while.
Sounds good, I completely missed the fact the fix would take some time to sync from bpf to bpf-next and until then the selftest will keep crashing.
I updated the selftest and will send in a v2 and while we are at it, will also fix
the typo Andrii found.
- KP
Quoted 3 lines
Thanks,
Daniel
Quoted 4 lines · KP Singh
The verifier allows
ARG_PTR_TO_BTF_IDhelper arguments to be NULL, so helper implementations need to check this before dereferencing them. This was already fixed for the socket storage helpers but not for task and inode.