1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef __TRACE_PROBE_KERNEL_H_ 3 #define __TRACE_PROBE_KERNEL_H_ 4 5 /* 6 * This depends on trace_probe.h, but can not include it due to 7 * the way trace_probe_tmpl.h is used by trace_kprobe.c and trace_eprobe.c. 8 * Which means that any other user must include trace_probe.h before including 9 * this file. 10 */ 11 /* Return the length of string -- including null terminal byte */ 12 static nokprobe_inline int 13 fetch_store_strlen_user(unsigned long addr) 14 { 15 const void __user *uaddr = (__force const void __user *)addr; 16 17 return strnlen_user_nofault(uaddr, MAX_STRING_SIZE); 18 } 19 20 /* Return the length of string -- including null terminal byte */ 21 static nokprobe_inline int 22 fetch_store_strlen(unsigned long addr) 23 { 24 int ret, len = 0; 25 u8 c; 26 27 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 28 if (addr < TASK_SIZE) 29 return fetch_store_strlen_user(addr); 30 #endif 31 32 do { 33 ret = copy_from_kernel_nofault(&c, (u8 *)addr + len, 1); 34 len++; 35 } while (c && ret == 0 && len < MAX_STRING_SIZE); 36 37 return (ret < 0) ? ret : len; 38 } 39 40 /* 41 * Fetch a null-terminated string from user. Caller MUST set *(u32 *)buf 42 * with max length and relative data location. 43 */ 44 static nokprobe_inline int 45 fetch_store_string_user(unsigned long addr, void *dest, void *base) 46 { 47 const void __user *uaddr = (__force const void __user *)addr; 48 int maxlen = get_loc_len(*(u32 *)dest); 49 void *__dest; 50 long ret; 51 52 if (unlikely(!maxlen)) 53 return -ENOMEM; 54 55 __dest = get_loc_data(dest, base); 56 57 ret = strncpy_from_user_nofault(__dest, uaddr, maxlen); 58 if (ret >= 0) 59 *(u32 *)dest = make_data_loc(ret, __dest - base); 60 61 return ret; 62 } 63 64 /* 65 * Fetch a null-terminated string. Caller MUST set *(u32 *)buf with max 66 * length and relative data location. 67 */ 68 static nokprobe_inline int 69 fetch_store_string(unsigned long addr, void *dest, void *base) 70 { 71 int maxlen = get_loc_len(*(u32 *)dest); 72 void *__dest; 73 long ret; 74 75 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 76 if ((unsigned long)addr < TASK_SIZE) 77 return fetch_store_string_user(addr, dest, base); 78 #endif 79 80 if (unlikely(!maxlen)) 81 return -ENOMEM; 82 83 __dest = get_loc_data(dest, base); 84 85 /* 86 * Try to get string again, since the string can be changed while 87 * probing. 88 */ 89 ret = strncpy_from_kernel_nofault(__dest, (void *)addr, maxlen); 90 if (ret >= 0) 91 *(u32 *)dest = make_data_loc(ret, __dest - base); 92 93 return ret; 94 } 95 96 static nokprobe_inline int 97 probe_mem_read_user(void *dest, void *src, size_t size) 98 { 99 const void __user *uaddr = (__force const void __user *)src; 100 101 return copy_from_user_nofault(dest, uaddr, size); 102 } 103 104 static nokprobe_inline int 105 probe_mem_read(void *dest, void *src, size_t size) 106 { 107 #ifdef CONFIG_ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE 108 if ((unsigned long)src < TASK_SIZE) 109 return probe_mem_read_user(dest, src, size); 110 #endif 111 return copy_from_kernel_nofault(dest, src, size); 112 } 113 114 #endif /* __TRACE_PROBE_KERNEL_H_ */ 115