Skip to content

Commit 5046acc

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-add-kfuncs-for-read-only-string-operations'
Viktor Malik says: ==================== bpf: Add kfuncs for read-only string operations String operations are commonly used in programming and BPF programs are no exception. Since it is cumbersome to reimplement them over and over, this series introduce kfuncs which provide the most common operations. For now, we only limit ourselves to functions which do not copy memory since these usually introduce undefined behaviour in case the source/destination buffers overlap which would have to be prevented by the verifier. The kernel already contains implementations for all of these, however, it is not possible to use them from BPF context. The main reason is that the verifier is not able to check that it is safe to access the entire string and that the string is null-terminated and the function won't loop forever. Therefore, the operations are open-coded using __get_kernel_nofault instead of plain dereference and bounded to at most XATTR_SIZE_MAX characters to make them safe. That allows to skip all the verfier checks for the passed-in strings as safety is ensured dynamically. All of the proposed functions return integers, even those that normally (in the kernel or libc) return pointers into the strings. The reason is that since the strings are generally treated as unsafe, the pointers couldn't be dereferenced anyways. So, instead, we return an index to the string and let user decide what to do with it. The integer APIs also allow to return various error codes when unexpected situations happen while processing the strings. The series include both positive and negative tests using the kfuncs. Changelog --------- Changes in v8: - Return -ENOENT (instead of -1) when "item not found" for relevant functions (Alexei). - Small adjustments of the string algorithms (Andrii). - Adapt comments to kernel style (Alexei). Changes in v7: - Disable negative tests passing NULL and 0x1 to kfuncs on s390 as they aren't relevant (see comment in string_kfuncs_failure1.c for details). Changes in v6: - Improve the third patch which allows to use macros in __retval in selftests. The previous solution broke several tests. Changes in v5: - Make all kfuncs return integers (Andrii). - Return -ERANGE when passing non-kernel pointers on arches with non-overlapping address spaces (Alexei). - Implement "unbounded" variants using the bounded ones (Andrii). - Add more negative test cases. Changes in v4 (all suggested by Andrii): - Open-code all the kfuncs, not just the unbounded variants. - Introduce `pagefault` lock guard to simplify the implementation - Return appropriate error codes (-E2BIG and -EFAULT) on failures - Const-ify all arguments and return values - Add negative test-cases Changes in v3: - Open-code unbounded variants with __get_kernel_nofault instead of dereference (suggested by Alexei). - Use the __sz suffix for size parameters in bounded variants (suggested by Eduard and Alexei). - Make tests more compact (suggested by Eduard). - Add benchmark. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents d83caf7 + e8763fb commit 5046acc

File tree

9 files changed

+615
-23
lines changed

9 files changed

+615
-23
lines changed

include/linux/uaccess.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ static inline bool pagefault_disabled(void)
296296
*/
297297
#define faulthandler_disabled() (pagefault_disabled() || in_atomic())
298298

299+
DEFINE_LOCK_GUARD_0(pagefault, pagefault_disable(), pagefault_enable())
300+
299301
#ifndef CONFIG_ARCH_HAS_SUBPAGE_FAULTS
300302

301303
/**

0 commit comments

Comments
 (0)