iov: Bypass usercopy hardening for kernel iterators
Subsystems (6)
RFC PATCH 1/1 iov: Bypass usercopy hardening for kernel iterators
From: Chuck Lever <[email protected]>
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in check_object_size() validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.
Split check_copy_size() so that copy_to_iter() can bypass the runtime check_object_size() call for kernel-only iterators (ITER_BVEC, ITER_KVEC). Existing callers of check_copy_size() are unaffected; user-backed iterators still receive the full usercopy validation.
This benefits all kernel consumers of copy_to_iter(), including the TCP receive path used by the NFS client and server, NVMe-TCP, and any other subsystem that uses ITER_BVEC or ITER_KVEC receive buffers.
--- include/linux/ucopysize.h | 10 +++++++++- include/linux/uio.h | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)
include/linux/ucopysize.h +9 -1
@@ -42,7 +42,7 @@ static inline void copy_overflow(int size, unsigned long count) } static __always_inline __must_check bool -check_copy_size (const void *addr,size_t bytes,bool is_source) +check_copy_size_nosec (const void *addr,size_t bytes,bool is_source) {int sz =__builtin_object_size (addr,0 );if (unlikely (sz >=0 && sz < bytes)) {
@@ -56,6 +56,14 @@ check_copy_size(const void *addr, size_t bytes, bool is_source) }if (WARN_ON_ONCE (bytes >INT_MAX ))return false; +return true; +} + +static __always_inline __must_check bool +check_copy_size (const void *addr,size_t bytes,bool is_source) +{ +if (!check_copy_size_nosec (addr, bytes, is_source)) +return false;check_object_size (addr, bytes, is_source);return true; }
include/linux/uio.h +7 -2
@@ -216,8 +216,13 @@ size_t copy_page_to_iter_nofault(struct page *page, unsigned offset,static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) { -if (check_copy_size (addr, bytes, true)) -return _copy_to_iter (addr, bytes, i); +if (user_backed_iter (i)) { +if (check_copy_size (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }else { +if (check_copy_size_nosec (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }return 0 ; }
Signature · 1 line
2.53.0
Quoted 6 lines · Chuck Lever
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.
I'm not sure I'd go as far as "no value". I could see an attack which managed to trick the kernel into copying past the end of a slab object and sending the contents of that buffer across the network to an attacker.
Or I guess in this case you're talking about copying _to_ a slab object. Then we could see a network attacker somewhow confusing the kernel into copying past the end of the object they allocated, overwriting slab metadata and/or the contents of the next object in the slab.
Limited value, sure. And the performance change you're showing here certainly isn't nothing!
Quoted 69 lines
Split
check_copy_size()so thatcopy_to_iter()can bypass the runtimecheck_object_size()call for kernel-only iterators (ITER_BVEC,ITER_KVEC). Existing callers ofcheck_copy_size()are unaffected; user-backed iterators still receive the full usercopy validation.This benefits all kernel consumers of
copy_to_iter(), including the TCP receive path used by the NFS client and server, NVMe-TCP, and any other subsystem that usesITER_BVECorITER_KVECreceive buffers.Signed-off-by Chuck Lever <[email protected]>--- include/linux/ucopysize.h | 10 +++++++++- include/linux/uio.h | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)include/linux/ucopysize.h +9
-1@@ -42,7 +42,7 @@ static inline void copy_overflow(int size, unsigned long count) } static __always_inline __must_check bool -check_copy_size (const void *addr,size_t bytes,bool is_source) +check_copy_size_nosec (const void *addr,size_t bytes,bool is_source) {int sz =__builtin_object_size (addr,0 );if (unlikely (sz >=0 && sz < bytes)) {@@ -56,6 +56,14 @@ check_copy_size(const void *addr, size_t bytes, bool is_source) }if (WARN_ON_ONCE (bytes >INT_MAX ))return false; +return true; +} + +static __always_inline __must_check bool +check_copy_size (const void *addr,size_t bytes,bool is_source) +{ +if (!check_copy_size_nosec (addr, bytes, is_source)) +return false;check_object_size (addr, bytes, is_source);return true; }include/linux/uio.h +7
-2@@ -216,8 +216,13 @@ size_t copy_page_to_iter_nofault(struct page *page, unsigned offset,static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) { -if (check_copy_size (addr, bytes, true)) -return _copy_to_iter (addr, bytes, i); +if (user_backed_iter (i)) { +if (check_copy_size (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }else { +if (check_copy_size_nosec (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }return 0 ; }Signature · 1 line
2.53.0
Quoted 12 lines · Matthew Wilcox
Quoted 6 lines · Chuck Lever
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.I'm not sure I'd go as far as "no value". I could see an attack which managed to trick the kernel into copying past the end of a slab object and sending the contents of that buffer across the network to an attacker.
Quoted 7 lines
Or I guess in this case you're talking about copying _to_ a slab object. Then we could see a network attacker somewhow confusing the kernel into copying past the end of the object they allocated, overwriting slab metadata and/or the contents of the next object in the slab.
Limited value, sure. And the performance change you're showing here certainly isn't nothing!
To be clear, I'm absolutely interested in not degrading our security posture. But NFSD (and other storage ULPs, for example) do a lot of internal data copying that could be more efficient.
I would place the "trick the kernel into copying past the end of a slab object" attack in the category of "you should sanitize your input better"... Perhaps the existing copy_to_iter protection is a general salve that could be replaced by something more narrow and less costly. </hand wave>
Signature · 1 line
Chuck Lever
Quoted 25 lines · Chuck Lever
Quoted 12 lines · Matthew Wilcox
Quoted 6 lines · Chuck Lever
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.I'm not sure I'd go as far as "no value". I could see an attack which managed to trick the kernel into copying past the end of a slab object and sending the contents of that buffer across the network to an attacker.
Quoted 7 lines
Or I guess in this case you're talking about copying _to_ a slab object. Then we could see a network attacker somewhow confusing the kernel into copying past the end of the object they allocated, overwriting slab metadata and/or the contents of the next object in the slab.
Limited value, sure. And the performance change you're showing here certainly isn't nothing!
To be clear, I'm absolutely interested in not degrading our security posture. But NFSD (and other storage ULPs, for example) do a lot of internal data copying that could be more efficient.
Quoted 5 lines
I would place the "trick the kernel into copying past the end of a slab object" attack in the category of "you should sanitize your input better"... Perhaps the existing
copy_to_iterprotection is a general salve that could be replaced by something more narrow and less costly. </hand wave>
As I understand it, and I'm sure Kees will correct me if I'm wrong, the hardened usercopy stuff is always "you should have sanitised your input before you got here"; it's never "yolo, just copy the amount the user asked for and if it's too much, the hardening will catch it".
I'm definitely open to the original patch, as well as other alternatives that narrow down the cases where we can prove that we're not doing anything wrong. I just want to be sure that we all understand what tradeoffs we're making and why.
Quoted 79 lines · Chuck Lever
From: Chuck Lever <[email protected]>
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.Split
check_copy_size()so thatcopy_to_iter()can bypass the runtimecheck_object_size()call for kernel-only iterators (ITER_BVEC,ITER_KVEC). Existing callers ofcheck_copy_size()are unaffected; user-backed iterators still receive the full usercopy validation.This benefits all kernel consumers of
copy_to_iter(), including the TCP receive path used by the NFS client and server, NVMe-TCP, and any other subsystem that usesITER_BVECorITER_KVECreceive buffers.Signed-off-by Chuck Lever <[email protected]>--- include/linux/ucopysize.h | 10 +++++++++- include/linux/uio.h | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)include/linux/ucopysize.h +1
-1@@ -42,7 +42,7 @@ static inline void copy_overflow(int size, unsigned long count) } static __always_inline __must_check bool -check_copy_size (const void *addr,size_t bytes,bool is_source) +check_copy_size_nosec (const void *addr,size_t bytes,bool is_source) {int sz =__builtin_object_size (addr,0 );include/linux/ucopysize.h +8
-0if (unlikely (sz >=0 && sz < bytes)) {@@ -56,6 +56,14 @@ check_copy_size(const void *addr, size_t bytes, bool is_source) }if (WARN_ON_ONCE (bytes >INT_MAX ))return false; +return true; +} + +static __always_inline __must_check bool +check_copy_size (const void *addr,size_t bytes,bool is_source) +{ +if (!check_copy_size_nosec (addr, bytes, is_source)) +return false;check_object_size (addr, bytes, is_source);return true;}
include/linux/uio.h +7
-2@@ -216,8 +216,13 @@ size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, static __always_inline __must_checksize_t copy_to_iter (const void *addr,size_t bytes,struct iov_iter *i) { -if (check_copy_size (addr, bytes, true)) -return _copy_to_iter (addr, bytes, i); +if (user_backed_iter (i)) { +if (check_copy_size (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }else { +if (check_copy_size_nosec (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }return 0 ; }Signature · 2 lines
-- 2.53.0
Ping: Any further thoughts on this? Al, Kees, Gustavo?
Signature · 1 line
Chuck Lever
Kees Cook
maintainerQuoted 15 lines · Chuck Lever
From: Chuck Lever <[email protected]>
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.Split
check_copy_size()so thatcopy_to_iter()can bypass the runtimecheck_object_size()call for kernel-only iterators (ITER_BVEC,ITER_KVEC). Existing callers ofcheck_copy_size()are unaffected; user-backed iterators still receive the full usercopy validation.
Quoted 4 lines
This benefits all kernel consumers of
copy_to_iter(), including the TCP receive path used by the NFS client and server, NVMe-TCP, and any other subsystem that usesITER_BVECorITER_KVECreceive buffers.
So, I'm not a big fan of this just because the whole point is to catch unexpected conditions, but there is a reasonable point to be made that this case shouldn't be covered by kernel/kernel copies.
Quoted 12 lines
Signed-off-by Chuck Lever <[email protected]>--- include/linux/ucopysize.h | 10 +++++++++- include/linux/uio.h | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)include/linux/ucopysize.h +0
-0
include/linux/ucopysize.h static inline void copy_overflow(int size, unsigned long count) · line 45
Quoted 5 lines
include/linux/ucopysize.h +1
-1@@ -42,7 +42,7 @@ static inline void copy_overflow(int size, unsigned long count) } static __always_inline __must_check bool -check_copy_size (const void *addr,size_t bytes,bool is_source) +check_copy_size_nosec (const void *addr,size_t bytes,bool is_source)
"nosec" is kind of ambiguous. Since this is doing the compile-time checks, how about naming this __compiletime_check_copy_size() or so?
Quoted 27 lines
include/linux/ucopysize.h +8
-0{int sz =__builtin_object_size (addr,0 );if (unlikely (sz >=0 && sz < bytes)) {@@ -56,6 +56,14 @@ check_copy_size(const void *addr, size_t bytes, bool is_source) }if (WARN_ON_ONCE (bytes >INT_MAX ))return false; +return true; +} + +static __always_inline __must_check bool +check_copy_size (const void *addr,size_t bytes,bool is_source) +{ +if (!check_copy_size_nosec (addr, bytes, is_source)) +return false;check_object_size (addr, bytes, is_source);return true; }include/linux/uio.h +0
-1@@ -216,8 +216,13 @@ size_t copy_page_to_iter_nofault(struct page *page, unsigned offset,static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) { -if (check_copy_size (addr, bytes, true))
include/linux/uio.h size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, · line 227
Quoted 10 lines
include/linux/uio.h +7
-1-return _copy_to_iter (addr, bytes, i); +if (user_backed_iter (i)) { +if (check_copy_size (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }else { +if (check_copy_size_nosec (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }return 0 ; }
This seems reasonable with the renaming, though I might come back some day and ask that this get a boot param or something (we have a big hammer boot param for usercopy checking already, but I like this more focused check).
Signature · 1 line
Kees Cook
Quoted 39 lines · Kees Cook
Quoted 15 lines · Chuck Lever
From: Chuck Lever <[email protected]>
Profiling NFSD under an iozone workload showed that hardened usercopy checks consume roughly 1.3% of CPU in the TCP receive path. The runtime check in
check_object_size()validates that copy buffers reside in expected slab regions, which is meaningful when data crosses the user/kernel boundary but adds no value when both source and destination are kernel addresses.Split
check_copy_size()so thatcopy_to_iter()can bypass the runtimecheck_object_size()call for kernel-only iterators (ITER_BVEC,ITER_KVEC). Existing callers ofcheck_copy_size()are unaffected; user-backed iterators still receive the full usercopy validation.Quoted 4 lines
This benefits all kernel consumers of
copy_to_iter(), including the TCP receive path used by the NFS client and server, NVMe-TCP, and any other subsystem that usesITER_BVECorITER_KVECreceive buffers.So, I'm not a big fan of this just because the whole point is to catch unexpected conditions, but there is a reasonable point to be made that this case shouldn't be covered by kernel/kernel copies.
Quoted 14 lines
Signed-off-by Chuck Lever <[email protected]>--- include/linux/ucopysize.h | 10 +++++++++- include/linux/uio.h | 9 +++++++-- 2 files changed, 16 insertions(+), 3 deletions(-)include/linux/ucopysize.h +0
-0@@ -42,7 +42,7 @@ static inline void copy_overflow(int size, unsigned long count) }
Quoted 6 lines
include/linux/ucopysize.h static inline void copy_overflow(int size, unsigned long count) · line 45
Quoted 3 lines
include/linux/ucopysize.h +1
-1static __always_inline __must_check bool -check_copy_size (const void *addr,size_t bytes,bool is_source) +check_copy_size_nosec (const void *addr,size_t bytes,bool is_source)"nosec" is kind of ambiguous. Since this is doing the compile-time checks, how about naming this
__compiletime_check_copy_size()or so?
No problem.
Quoted 34 lines
Quoted 34 lines
include/linux/ucopysize.h +8
-0{int sz =__builtin_object_size (addr,0 );if (unlikely (sz >=0 && sz < bytes)) {@@ -56,6 +56,14 @@ check_copy_size(const void *addr, size_t bytes, bool is_source) }if (WARN_ON_ONCE (bytes >INT_MAX ))return false; +return true; +} + +static __always_inline __must_check bool +check_copy_size (const void *addr,size_t bytes,bool is_source) +{ +if (!check_copy_size_nosec (addr, bytes, is_source)) +return false;check_object_size (addr, bytes, is_source);return true; }include/linux/uio.h +6
-2@@ -216,8 +216,13 @@ size_t copy_page_to_iter_nofault(struct page *page, unsigned offset,static __always_inline __must_check size_t copy_to_iter(const void *addr, size_t bytes, struct iov_iter *i) { -if (check_copy_size (addr, bytes, true)) -return _copy_to_iter (addr, bytes, i); +if (user_backed_iter (i)) { +if (check_copy_size (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i); + }else { +if (check_copy_size_nosec (addr, bytes, true)) +return _copy_to_iter (addr, bytes, i);
Quoted 9 lines
include/linux/uio.h size_t copy_page_to_iter_nofault(struct page *page, unsigned offset, · line 227
Quoted 3 lines
include/linux/uio.h +1
-0+ }return 0 ; }This seems reasonable with the renaming, though I might come back some day and ask that this get a boot param or something (we have a big hammer boot param for usercopy checking already, but I like this more focused check).
Thanks for having a look. An additional question is whether the "copy from" direction needs similar treatment. Performance analysis found "copy to" was an issue for my particular workload (NFSD) but it's plausible that "copy from" should be handled similarly.
Signature · 1 line
Chuck Lever