kallsyms: make kallsym APIs more safe with scnprintf
Subsystems (15)
A message that is not in the archive (sent off-list or not yet received): CGME20220520083715epcas5p400b11adef4d540756c985feb20ba29bc@epcas5p4.samsung.com
cover kallsyms: make kallsym APIs more safe with scnprintf
kallsyms functionality depends on KSYM_NAME_LEN directly. but if user passed array length lesser than it, sprintf can cause issues of buffer overflow attack.
So changing *sprint* and *lookup* APIs in this patch set to have buffer size as an argument and replacing sprintf with scnprintf.
patch 1 and 2 can be clubbed, but then it will be difficult to review, so patch 1 changes prototype only and patch 2 includes passed argument usage.
Patch 3 and patch 5 are bug fixes.
Patch 1, 2 and 4 are changing prorotypes.
Tried build and kallsyms test on ARM64 environment. APIs are called at multiple places. So build can be failed if updation missed at any place. lets see if autobot reports any build failure with any config combination.
[ 12.247313] ps function_check [crash] [ 12.247906] pS function_check+0x4/0x40 [crash] [ 12.247999] pSb function_check+0x4/0x40 [crash df48d71893b7fb2688ac9739346449e89e8a76ca] [ 12.248092] pB function_check+0x4/0x40 [crash] [ 12.248190] pBb function_check+0x4/0x40 [crash df48d71893b7fb2688ac9739346449e89e8a76ca] ... [ 12.261175] Call trace: [ 12.261361] function_2+0x74/0x88 [crash df48d71893b7fb2688ac9739346449e89e8a76ca] [ 12.261859] function_1+0x10/0x1c [crash df48d71893b7fb2688ac9739346449e89e8a76ca] [ 12.262237] hello_init+0x24/0x34 [crash df48d71893b7fb2688ac9739346449e89e8a76ca] [ 12.262603] do_one_initcall+0x54/0x1c8 [ 12.262803] do_init_module+0x44/0x1d0 [ 12.262992] load_module+0x1688/0x19f0 [ 12.263179] __do_sys_init_module+0x1a0/0x210 [ 12.263387] __arm64_sys_init_module+0x1c/0x28 [ 12.263596] invoke_syscall+0x44/0x108 [ 12.263788] el0_svc_common.constprop.0+0x44/0xf0 [ 12.264014] do_el0_svc_compat+0x1c/0x50 [ 12.264209] el0_svc_compat+0x2c/0x88 [ 12.264397] el0t_32_sync_handler+0x90/0x140 [ 12.264600] el0t_32_sync+0x190/0x194
Maninder Singh, Onkarnath (5):
kallsyms: pass buffer size in sprint_* APIs
kallsyms: replace sprintf with scprintf arch:hexagon/powerpc: use KSYM_NAME_LEN as array size
kallsyms: pass buffer size argument in *lookup* APIs
kallsyms: remove unsed API lookup_symbol_attrs
arch/hexagon/kernel/traps.c | 4 +- arch/powerpc/xmon/xmon.c | 6 +- arch/s390/lib/test_unwind.c | 2 +- drivers/scsi/fnic/fnic_trace.c | 8 +-- fs/proc/base.c | 2 +- include/linux/kallsyms.h | 34 +++++------ include/linux/module.h | 14 ++--- init/main.c | 2 +- kernel/debug/kdb/kdb_support.c | 2 +- kernel/kallsyms.c | 92 ++++++++++++------------------ kernel/kprobes.c | 4 +- kernel/locking/lockdep.c | 8 +-- kernel/locking/lockdep_internals.h | 2 +- kernel/locking/lockdep_proc.c | 4 +- kernel/module/kallsyms.c | 36 ++---------- kernel/trace/ftrace.c | 9 +-- kernel/trace/trace_kprobe.c | 2 +- kernel/trace/trace_output.c | 4 +- kernel/trace/trace_syscalls.c | 2 +- lib/vsprintf.c | 10 ++-- 20 files changed, 93 insertions(+), 154 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/5 kallsyms: pass buffer size in sprint_* APIs
As of now sprint_* APIs don't pass buffer size as an argument and use sprintf directly.
To replace dangerous sprintf API to scnprintf,
buffer size is required in arguments.
--- arch/s390/lib/test_unwind.c | 2 +- drivers/scsi/fnic/fnic_trace.c | 8 ++++---- include/linux/kallsyms.h | 20 ++++++++++---------- init/main.c | 2 +- kernel/kallsyms.c | 27 ++++++++++++++++----------- kernel/trace/trace_output.c | 2 +- lib/vsprintf.c | 10 +++++----- 7 files changed, 38 insertions(+), 33 deletions(-)
arch/s390/lib/test_unwind.c +1 -1
@@ -75,7 +75,7 @@ static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs, ret = -EINVAL ;break ; } -sprint_symbol (sym, addr); +sprint_symbol (sym,KSYM_SYMBOL_LEN , addr);if (bt_pos <BT_BUF_SIZE ) { bt_pos += snprintf(bt + bt_pos, BT_BUF_SIZE - bt_pos, state.reliable ? " [%-7s%px] %pSR\n" :
drivers/scsi/fnic/fnic_trace.c +4 -4
@@ -128,10 +128,10 @@ int fnic_get_trace_data(fnic_dbgfs_t *fnic_dbgfs_prt) }/* Convert function pointer to function name */ if (sizeof (unsigned long ) <8 ) { -sprint_symbol (str, tbp->fnaddr.low); +sprint_symbol (str,KSYM_SYMBOL_LEN , tbp->fnaddr.low);jiffies_to_timespec64 (tbp->timestamp.low, &val); }else { -sprint_symbol (str, tbp->fnaddr.val); +sprint_symbol (str,KSYM_SYMBOL_LEN , tbp->fnaddr.val);jiffies_to_timespec64 (tbp->timestamp.val, &val); }/*
@@ -170,10 +170,10 @@ int fnic_get_trace_data(fnic_dbgfs_t *fnic_dbgfs_prt) }/* Convert function pointer to function name */ if (sizeof (unsigned long ) <8 ) { -sprint_symbol (str, tbp->fnaddr.low); +sprint_symbol (str,KSYM_SYMBOL_LEN , tbp->fnaddr.low);jiffies_to_timespec64 (tbp->timestamp.low, &val); }else { -sprint_symbol (str, tbp->fnaddr.val); +sprint_symbol (str,KSYM_SYMBOL_LEN , tbp->fnaddr.val);jiffies_to_timespec64 (tbp->timestamp.val, &val); }/*
include/linux/kallsyms.h +10 -10
@@ -84,11 +84,11 @@ const char *kallsyms_lookup(unsigned long addr, char **modname, char *namebuf);/* Look up a kernel symbol and return it in a text buffer. */ -extern int sprint_symbol (char *buffer,unsigned long address); -extern int sprint_symbol_build_id (char *buffer,unsigned long address); -extern int sprint_symbol_no_offset (char *buffer,unsigned long address); -extern int sprint_backtrace (char *buffer,unsigned long address); -extern int sprint_backtrace_build_id (char *buffer,unsigned long address); +extern int sprint_symbol (char *buffer,size_t size,unsigned long address); +extern int sprint_symbol_build_id (char *buffer,size_t size,unsigned long address); +extern int sprint_symbol_no_offset (char *buffer,size_t size,unsigned long address); +extern int sprint_backtrace (char *buffer,size_t size,unsigned long address); +extern int sprint_backtrace_build_id (char *buffer,size_t size,unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname);int lookup_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name);
@@ -118,31 +118,31 @@ static inline const char *kallsyms_lookup(unsigned long addr,return NULL ; } -static inline int sprint_symbol (char *buffer,unsigned long addr) +static inline int sprint_symbol (char *buffer,size_t size,unsigned long addr) { *buffer ='\0' ;return 0 ; } -static inline int sprint_symbol_build_id (char *buffer,unsigned long address) +static inline int sprint_symbol_build_id (char *buffer,size_t size,unsigned long address) { *buffer ='\0' ;return 0 ; } -static inline int sprint_symbol_no_offset (char *buffer,unsigned long addr) +static inline int sprint_symbol_no_offset (char *buffer,size_t size,unsigned long addr) { *buffer ='\0' ;return 0 ; } -static inline int sprint_backtrace (char *buffer,unsigned long addr) +static inline int sprint_backtrace (char *buffer,size_t size,unsigned long addr) { *buffer ='\0' ;return 0 ; } -static inline int sprint_backtrace_build_id (char *buffer,unsigned long addr) +static inline int sprint_backtrace_build_id (char *buffer,size_t size,unsigned long addr) { *buffer ='\0' ;return 0 ;
init/main.c +1 -1
@@ -1207,7 +1207,7 @@ static bool __init_or_module initcall_blacklisted(initcall_t fn)return false; addr = (unsigned long )dereference_function_descriptor (fn); -sprint_symbol_no_offset (fn_name, addr); +sprint_symbol_no_offset (fn_name,KSYM_SYMBOL_LEN , addr);/* * fn will be "function_name [module_name]" where [module_name] is not
kernel/kallsyms.c +16 -11
@@ -459,7 +459,7 @@ int lookup_symbol_attrs(unsigned long addr, unsigned long *size, }/* Look up a kernel symbol and return it in a text buffer. */ -static int __sprint_symbol (char *buffer,unsigned long address, +static int __sprint_symbol (char *buffer,size_t buf_size,unsigned long address,int symbol_offset,int add_offset,int add_buildid) {char *modname;
@@ -502,6 +502,7 @@ static int __sprint_symbol(char *buffer, unsigned long address,/** * sprint_symbol - Look up a kernel symbol and return it in a text buffer * @buffer: buffer to be stored +* @size: size of buffer * @address: address to lookup * * This function looks up a kernel symbol with @address and stores its name,
@@ -510,15 +511,16 @@ static int __sprint_symbol(char *buffer, unsigned long address,* * This function returns the number of bytes stored in @buffer. */ -int sprint_symbol (char *buffer,unsigned long address) +int sprint_symbol (char *buffer,size_t size,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,1 ,0 ); +return __sprint_symbol (buffer, size, address,0 ,1 ,0 ); }EXPORT_SYMBOL_GPL (sprint_symbol);/** * sprint_symbol_build_id - Look up a kernel symbol and return it in a text buffer * @buffer: buffer to be stored +* @size: size of buffer * @address: address to lookup * * This function looks up a kernel symbol with @address and stores its name,
@@ -527,15 +529,16 @@ EXPORT_SYMBOL_GPL(sprint_symbol);* * This function returns the number of bytes stored in @buffer. */ -int sprint_symbol_build_id (char *buffer,unsigned long address) +int sprint_symbol_build_id (char *buffer,size_t size,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,1 ,1 ); +return __sprint_symbol (buffer, size, address,0 ,1 ,1 ); }EXPORT_SYMBOL_GPL (sprint_symbol_build_id);/** * sprint_symbol_no_offset - Look up a kernel symbol and return it in a text buffer * @buffer: buffer to be stored +* @size: size of buffer * @address: address to lookup * * This function looks up a kernel symbol with @address and stores its name
@@ -544,15 +547,16 @@ EXPORT_SYMBOL_GPL(sprint_symbol_build_id);* * This function returns the number of bytes stored in @buffer. */ -int sprint_symbol_no_offset (char *buffer,unsigned long address) +int sprint_symbol_no_offset (char *buffer,size_t size,unsigned long address) { -return __sprint_symbol (buffer, address,0 ,0 ,0 ); +return __sprint_symbol (buffer, size, address,0 ,0 ,0 ); }EXPORT_SYMBOL_GPL (sprint_symbol_no_offset);/** * sprint_backtrace - Look up a backtrace symbol and return it in a text buffer * @buffer: buffer to be stored +* @size: size of buffer * @address: address to lookup * * This function is for stack backtrace and does the same thing as
@@ -564,14 +568,15 @@ EXPORT_SYMBOL_GPL(sprint_symbol_no_offset);* * This function returns the number of bytes stored in @buffer. */ -int sprint_backtrace (char *buffer,unsigned long address) +int sprint_backtrace (char *buffer,size_t size,unsigned long address) { -return __sprint_symbol (buffer, address,-1 ,1 ,0 ); +return __sprint_symbol (buffer, size, address,-1 ,1 ,0 ); }/** * sprint_backtrace_build_id - Look up a backtrace symbol and return it in a text buffer * @buffer: buffer to be stored +* @size: size of buffer * @address: address to lookup * * This function is for stack backtrace and does the same thing as
@@ -584,9 +589,9 @@ int sprint_backtrace(char *buffer, unsigned long address)* * This function returns the number of bytes stored in @buffer. */ -int sprint_backtrace_build_id (char *buffer,unsigned long address) +int sprint_backtrace_build_id (char *buffer,size_t size,unsigned long address) { -return __sprint_symbol (buffer, address,-1 ,1 ,1 ); +return __sprint_symbol (buffer, size, address,-1 ,1 ,1 ); }/* To avoid using get_symbol_offset for every symbol, we carry prefix along. */
kernel/trace/trace_output.c +1 -1
@@ -362,7 +362,7 @@ trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)const char *name;if (offset) -sprint_symbol (str, address); +sprint_symbol (str,KSYM_SYMBOL_LEN , address);else kallsyms_lookup (address,NULL ,NULL ,NULL , str); name =kretprobed (str, address);
lib/vsprintf.c +5 -5
@@ -991,15 +991,15 @@ char *symbol_string(char *buf, char *end, void *ptr,#ifdef CONFIG_KALLSYMS if (*fmt =='B' && fmt[1 ] =='b' ) -sprint_backtrace_build_id (sym, value); +sprint_backtrace_build_id (sym,KSYM_SYMBOL_LEN , value);else if (*fmt =='B' ) -sprint_backtrace (sym, value); +sprint_backtrace (sym,KSYM_SYMBOL_LEN , value);else if (*fmt =='S' && (fmt[1 ] =='b' || (fmt[1 ] =='R' && fmt[2 ] =='b' ))) -sprint_symbol_build_id (sym, value); +sprint_symbol_build_id (sym,KSYM_SYMBOL_LEN , value);else if (*fmt !='s' ) -sprint_symbol (sym, value); +sprint_symbol (sym,KSYM_SYMBOL_LEN , value);else -sprint_symbol_no_offset (sym, value); +sprint_symbol_no_offset (sym,KSYM_SYMBOL_LEN , value);return string_nocheck (buf, end, sym, spec);#else
Signature · 1 line
2.17.1
Waiman Long
reviewerRe: [PATCH 1/5] kallsyms: pass buffer size in sprint_* APIs
Quoted 24 lines · Maninder Singh
As of now sprint_* APIs don't pass buffer size as an argument and use sprintf directly.
To replace dangerous sprintf API to scnprintf,
buffer size is required in arguments.Co-developed-by Onkarnath <[email protected]>Signed-off-by Onkarnath <[email protected]>Signed-off-by Maninder Singh <[email protected]>--- arch/s390/lib/test_unwind.c | 2 +- drivers/scsi/fnic/fnic_trace.c | 8 ++++---- include/linux/kallsyms.h | 20 ++++++++++---------- init/main.c | 2 +- kernel/kallsyms.c | 27 ++++++++++++++++----------- kernel/trace/trace_output.c | 2 +- lib/vsprintf.c | 10 +++++----- 7 files changed, 38 insertions(+), 33 deletions(-)arch/s390/lib/test_unwind.c +0
-0
arch/s390/lib/test_unwind.c static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs, · line 78
Quoted 5 lines
arch/s390/lib/test_unwind.c +1
-1@@ -75,7 +75,7 @@ static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs, ret = -EINVAL ;break ; } -sprint_symbol (sym, addr); +sprint_symbol (sym,KSYM_SYMBOL_LEN , addr);
Instead of hardcoding KSYM_SYMBOL_LEN everywhere, will it better to hide it like this:
extern int __sprint_symbol(char *buffer, size_t size, unsigned long address); #define sprint_symbol(buf, addr) __sprint_symbol(buf, sizeof(buf), addr)
Or you can use sizeof(buf) directly instead of KSYM_SYMBOL_LEN.
Cheers,
Longman
Andy Shevchenko
reviewerRe: [PATCH 1/5] kallsyms: pass buffer size in sprint_* APIs
Quoted 1 line · Waiman Long
On 5/20/22 04:36, Maninder Singh wrote:
...
Quoted 6 lines
arch/s390/lib/test_unwind.c static noinline int test_unwind(struct task_struct *task, struct pt_regs *regs, · line 78
Quoted 2 lines
arch/s390/lib/test_unwind.c +1
-1-sprint_symbol (sym, addr); +sprint_symbol (sym,KSYM_SYMBOL_LEN , addr);Instead of hardcoding
KSYM_SYMBOL_LENeverywhere, will it better to hide it like this:
Quoted 6 lines
extern int
__sprint_symbol(char *buffer, size_t size, unsigned long address);
#definesprint_symbol(buf, addr)__sprint_symbol(buf, sizeof(buf), addr)Or you can use sizeof(buf) directly instead of
KSYM_SYMBOL_LEN.
This assumes that buf is defined as char [], which might be not always the case. If you are going with the macro, than ARRAY_SIZE() seems appropriate to perform a check against the above mentioned constraint.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 2/5 kallsyms: replace sprintf with scnprintf
In reply to Maninder Singhreplace sprintf API with scnprintf which prevents buffer overflow.
--- kernel/kallsyms.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
kernel/kallsyms.c +7 -6
@@ -472,28 +472,29 @@ static int __sprint_symbol(char *buffer, size_t buf_size, unsigned long address, name =kallsyms_lookup_buildid (address, &size, &offset, &modname, &buildid, buffer);if (!name) -return sprintf (buffer,"0x%lx" , address - symbol_offset); +return scnprintf (buffer, buf_size,"0x%lx" , address - symbol_offset);if (name != buffer) -strcpy (buffer, name); +strncpy (buffer, name, buf_size); + len =strlen (buffer); offset -= symbol_offset;if (add_offset) - len +=sprintf (buffer + len,"+%#lx/%#lx" , offset, size); + len +=scnprintf (buffer + len, buf_size - len,"+%#lx/%#lx" , offset, size);if (modname) { - len +=sprintf (buffer + len," [%s" , modname); + len +=scnprintf (buffer + len, buf_size - len," [%s" , modname);#if IS_ENABLED (CONFIG_STACKTRACE_BUILD_ID )if (add_buildid && buildid) {/* build ID should match length of sprintf */ #if IS_ENABLED (CONFIG_MODULES )static_assert (sizeof (typeof_member (struct module ,build_id )) ==20 );#endif - len +=sprintf (buffer + len," %20phN" , buildid); + len +=scnprintf (buffer + len, buf_size - len," %20phN" , buildid); }#endif - len +=sprintf (buffer + len,"]" ); + len +=scnprintf (buffer + len, buf_size - len,"]" ); }return len;
Signature · 1 line
2.17.1
PATCH 3/5 arch:hexagon/powerpc: use KSYM_NAME_LEN as array size
In reply to Maninder Singhkallsyms_lookup which in turn calls for kallsyms_lookup_buildid() writes on index "KSYM_NAME_LEN - 1".
Thus array size should be KSYM_NAME_LEN.
for powerpc and hexagon it was defined as "128" directly. and commit '61968dbc2d5d' changed define value to 512, So both were missed to update with new size.
--- arch/hexagon/kernel/traps.c | 2 +- arch/powerpc/xmon/xmon.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/hexagon/kernel/traps.c +1 -1
@@ -82,7 +82,7 @@ static void do_show_stack(struct task_struct *task, unsigned long *fp,const char *name =NULL ;unsigned long *newfp;unsigned long low, high; -char tmpstr[128 ]; +char tmpstr[KSYM_NAME_LEN ];char *modname;int i;
arch/powerpc/xmon/xmon.c +1 -1
@@ -91,7 +91,7 @@ static unsigned long ndump = 64;static unsigned long nidump =16 ;static unsigned long ncsum =4096 ;static int termch; -static char tmpstr[128 ]; +static char tmpstr[KSYM_NAME_LEN ];static int tracing_enabled;static long bus_error_jmp[JMP_BUF_LEN ];
Signature · 1 line
2.17.1
PATCH 4/5 kallsyms: pass buffer size argument in *lookup* APIs
In reply to Maninder SinghAlthough *lookup* APIs are safe, but better to pass size as an argument rather than using define KSYM_NAME_LEN. Because it can cause issue if called with lesser array size.
--- arch/hexagon/kernel/traps.c | 2 +- arch/powerpc/xmon/xmon.c | 4 ++-- fs/proc/base.c | 2 +- include/linux/kallsyms.h | 8 ++++---- include/linux/module.h | 8 ++++---- kernel/debug/kdb/kdb_support.c | 2 +- kernel/kallsyms.c | 24 ++++++++++++------------ kernel/kprobes.c | 4 ++-- kernel/locking/lockdep.c | 8 ++++---- kernel/locking/lockdep_internals.h | 2 +- kernel/locking/lockdep_proc.c | 4 ++-- kernel/module/kallsyms.c | 8 ++++---- kernel/trace/ftrace.c | 9 +++++---- kernel/trace/trace_kprobe.c | 2 +- kernel/trace/trace_output.c | 2 +- kernel/trace/trace_syscalls.c | 2 +- 16 files changed, 46 insertions(+), 45 deletions(-)
arch/hexagon/kernel/traps.c +1 -1
@@ -118,7 +118,7 @@ static void do_show_stack(struct task_struct *task, unsigned long *fp,for (i =0 ; i < kstack_depth_to_print; i++) { - name =kallsyms_lookup (ip, &size, &offset, &modname, tmpstr); + name =kallsyms_lookup (ip, &size, &offset, &modname, tmpstr,KSYM_NAME_LEN );printk ("%s[%p] 0x%lx: %s + 0x%lx" , loglvl, fp, ip, name, offset);if (((unsigned long ) fp < low) || (high < (unsigned long ) fp))
arch/powerpc/xmon/xmon.c +2 -2
@@ -1710,7 +1710,7 @@ static void get_function_bounds(unsigned long pc, unsigned long *startp,if (setjmp (bus_error_jmp) ==0 ) { catch_memory_errors =1 ;sync (); - name =kallsyms_lookup (pc, &size, &offset,NULL , tmpstr); + name =kallsyms_lookup (pc, &size, &offset,NULL , tmpstr,KSYM_NAME_LEN );if (name !=NULL ) { *startp = pc - offset; *endp = pc - offset + size;
@@ -3730,7 +3730,7 @@ static void xmon_print_symbol(unsigned long address, const char *mid, catch_memory_errors =1 ;sync (); name =kallsyms_lookup (address, &size, &offset, &modname, - tmpstr); + tmpstr,KSYM_NAME_LEN );sync ();/* wait a little while to see if we get a machine check */ __delay (200 );
fs/proc/base.c +1 -1
@@ -392,7 +392,7 @@ static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, goto print0; wchan =get_wchan (task); -if (wchan && !lookup_symbol_name (wchan, symname)) { +if (wchan && !lookup_symbol_name (wchan, symname,KSYM_NAME_LEN )) {seq_puts (m, symname);return 0 ; }
include/linux/kallsyms.h +4 -4
@@ -81,7 +81,7 @@ extern int kallsyms_lookup_size_offset(unsigned long addr,const char *kallsyms_lookup (unsigned long addr,unsigned long *symbolsize,unsigned long *offset, -char **modname,char *namebuf); +char **modname,char *namebuf,size_t size);/* Look up a kernel symbol and return it in a text buffer. */ extern int sprint_symbol (char *buffer,size_t size,unsigned long address);
@@ -90,7 +90,7 @@ extern int sprint_symbol_no_offset(char *buffer, size_t size, unsigned long addrextern int sprint_backtrace (char *buffer,size_t size,unsigned long address);extern int sprint_backtrace_build_id (char *buffer,size_t size,unsigned long address); -int lookup_symbol_name (unsigned long addr,char *symname); +int lookup_symbol_name (unsigned long addr,char *symname,size_t size);int lookup_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name);/* How and when do we show kallsyms values? */
@@ -113,7 +113,7 @@ static inline int kallsyms_lookup_size_offset(unsigned long addr,static inline const char *kallsyms_lookup (unsigned long addr,unsigned long *symbolsize,unsigned long *offset, -char **modname,char *namebuf) +char **modname,char *namebuf,size_t size) {return NULL ; }
@@ -148,7 +148,7 @@ static inline int sprint_backtrace_build_id(char *buffer, size_t size, unsignedreturn 0 ; } -static inline int lookup_symbol_name (unsigned long addr,char *symname) +static inline int lookup_symbol_name (unsigned long addr,char *symname,size_t size) {return -ERANGE ; }
include/linux/module.h +4 -4
@@ -656,8 +656,8 @@ const char *module_address_lookup(unsigned long addr,unsigned long *symbolsize, unsigned long *offset, char **modname, const unsigned char **modbuildid, - char *namebuf); -int lookup_module_symbol_name (unsigned long addr,char *symname); + char *namebuf, size_t buf_size); +int lookup_module_symbol_name (unsigned long addr,char *symname,size_t size);int lookup_module_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name);int register_module_notifier (struct notifier_block *nb);
@@ -756,12 +756,12 @@ static inline const char *module_address_lookup(unsigned long addr,unsigned long *offset, char **modname, const unsigned char **modbuildid, - char *namebuf) + char *namebuf, size_t buf_size) { return NULL; } -static inline int lookup_module_symbol_name (unsigned long addr,char *symname) +static inline int lookup_module_symbol_name (unsigned long addr,char *symname,size_t size) {return -ERANGE ; }
kernel/debug/kdb/kdb_support.c +1 -1
@@ -92,7 +92,7 @@ int kdbnearsym(unsigned long addr, kdb_symtab_t *symtab) goto out; symtab->sym_name =kallsyms_lookup (addr, &symbolsize , &offset, - (char **)(&symtab->mod_name), namebuf); + (char **)(&symtab->mod_name), namebuf,KSYM_NAME_LEN );if (offset >8 *1024 *1024 ) { symtab->sym_name =NULL ; addr = offset = symbolsize =0 ;
kernel/kallsyms.c +12 -12
@@ -342,18 +342,18 @@ int kallsyms_lookup_size_offset(unsigned long addr, unsigned long *symbolsize,get_symbol_pos (addr, symbolsize, offset);return 1 ; } -return !!module_address_lookup (addr, symbolsize, offset,NULL ,NULL , namebuf) || +return !!module_address_lookup (addr, symbolsize, offset,NULL ,NULL , namebuf,KSYM_NAME_LEN ) || !!__bpf_address_lookup (addr, symbolsize, offset, namebuf); }static const char *kallsyms_lookup_buildid (unsigned long addr,unsigned long *symbolsize,unsigned long *offset,char **modname, -const unsigned char **modbuildid,char *namebuf) +const unsigned char **modbuildid,char *namebuf,size_t size) {const char *ret; - namebuf[KSYM_NAME_LEN -1 ] =0 ; + namebuf[size -1 ] =0 ; namebuf[0 ] =0 ;if (is_ksym_addr (addr)) {
@@ -362,7 +362,7 @@ static const char *kallsyms_lookup_buildid(unsigned long addr, pos =get_symbol_pos (addr, symbolsize, offset);/* Grab name */ kallsyms_expand_symbol (get_symbol_offset (pos), - namebuf,KSYM_NAME_LEN ); + namebuf, size);if (modname) *modname =NULL ;if (modbuildid)
@@ -374,7 +374,7 @@ static const char *kallsyms_lookup_buildid(unsigned long addr,/* See if it's in a module or a BPF JITed image. */ ret =module_address_lookup (addr, symbolsize, offset, - modname, modbuildid, namebuf); + modname, modbuildid, namebuf, size);if (!ret) ret =bpf_address_lookup (addr, symbolsize, offset, modname, namebuf);
@@ -398,18 +398,18 @@ static const char *kallsyms_lookup_buildid(unsigned long addr,const char *kallsyms_lookup (unsigned long addr,unsigned long *symbolsize,unsigned long *offset, -char **modname,char *namebuf) +char **modname,char *namebuf,size_t size) {return kallsyms_lookup_buildid (addr, symbolsize, offset, modname, -NULL , namebuf); +NULL , namebuf, size); } -int lookup_symbol_name (unsigned long addr,char *symname) +int lookup_symbol_name (unsigned long addr,char *symname,size_t size) {int res; symname[0 ] ='\0' ; - symname[KSYM_NAME_LEN -1 ] ='\0' ; + symname[size -1 ] ='\0' ;if (is_ksym_addr (addr)) {unsigned long pos;
@@ -417,11 +417,11 @@ int lookup_symbol_name(unsigned long addr, char *symname) pos =get_symbol_pos (addr,NULL ,NULL );/* Grab name */ kallsyms_expand_symbol (get_symbol_offset (pos), - symname,KSYM_NAME_LEN ); + symname, size); goto found; }/* See if it's in a module. */ - res =lookup_module_symbol_name (addr, symname); + res =lookup_module_symbol_name (addr, symname, size);if (res)return res;
@@ -470,7 +470,7 @@ static int __sprint_symbol(char *buffer, size_t buf_size, unsigned long address, address += symbol_offset; name =kallsyms_lookup_buildid (address, &size, &offset, &modname, &buildid, - buffer); + buffer, buf_size);if (!name)return scnprintf (buffer, buf_size,"0x%lx" , address - symbol_offset);
kernel/kprobes.c +2 -2
@@ -1478,7 +1478,7 @@ bool within_kprobe_blacklist(unsigned long addr)return true;/* Check if the address is on a suffixed-symbol */ -if (!lookup_symbol_name (addr, symname)) { +if (!lookup_symbol_name (addr, symname,KSYM_NAME_LEN )) { p =strchr (symname,'.' );if (!p)return false;
@@ -2806,7 +2806,7 @@ static int show_kprobe_addr(struct seq_file *pi, void *v)preempt_disable ();hlist_for_each_entry_rcu (p, head, hlist) { sym =kallsyms_lookup ((unsigned long )p->addr,NULL , - &offset, &modname, namebuf); + &offset, &modname, namebuf,KSYM_NAME_LEN );if (kprobe_aggrprobe (p)) {list_for_each_entry_rcu (kp, &p->list, list)report_probe (pi, kp, sym, offset, modname, p);
kernel/locking/lockdep.c +4 -4
@@ -659,9 +659,9 @@ static const char *usage_str[] = };#endif -const char *__get_key_name (const struct lockdep_subclass_key *key,char *str) +const char *__get_key_name (const struct lockdep_subclass_key *key,char *str,size_t size) { -return kallsyms_lookup ((unsigned long )key,NULL ,NULL ,NULL , str); +return kallsyms_lookup ((unsigned long )key,NULL ,NULL ,NULL , str, size); }static inline unsigned long lock_flag (enum lock_usage_bit bit)
@@ -715,7 +715,7 @@ static void __print_lock_name(struct lock_class *class) name = class->name;if (!name) { - name =__get_key_name (class->key, str); + name =__get_key_name (class->key, str,KSYM_NAME_LEN );printk (KERN_CONT "%s" , name); }else {printk (KERN_CONT "%s" , name);
@@ -746,7 +746,7 @@ static void print_lockdep_cache(struct lockdep_map *lock) name = lock->name;if (!name) - name =__get_key_name (lock->key->subkeys, str); + name =__get_key_name (lock->key->subkeys, str,KSYM_NAME_LEN );printk (KERN_CONT "%s" , name); }
kernel/locking/lockdep_internals.h +1 -1
@@ -129,7 +129,7 @@ extern void get_usage_chars(struct lock_class *class, char usage[LOCK_USAGE_CHARS]);extern const char *__get_key_name (const struct lockdep_subclass_key *key, -char *str); +char *str,size_t size);struct lock_class *lock_chain_get_class (struct lock_chain *chain,int i);
kernel/locking/lockdep_proc.c +2 -2
@@ -63,7 +63,7 @@ static void print_name(struct seq_file *m, struct lock_class *class)const char *name = class->name;if (!name) { - name =__get_key_name (class->key, str); + name =__get_key_name (class->key, str,KSYM_NAME_LEN );seq_printf (m,"%s" , name); }else {seq_printf (m,"%s" , name);
@@ -485,7 +485,7 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)char str[KSYM_NAME_LEN ];const char *key_name; - key_name =__get_key_name (ckey, str); + key_name =__get_key_name (ckey, str,KSYM_NAME_LEN );snprintf (name, namelen,"%s" , key_name); }else {snprintf (name, namelen,"%s" , cname);
kernel/module/kallsyms.c +4 -4
@@ -320,7 +320,7 @@ const char *module_address_lookup(unsigned long addr,unsigned long *offset, char **modname, const unsigned char **modbuildid, - char *namebuf) + char *namebuf, size_t buf_size) { const char *ret = NULL;struct module *mod;
@@ -342,7 +342,7 @@ const char *module_address_lookup(unsigned long addr, }/* Make a copy in here where it's safe */ if (ret) { -strncpy (namebuf, ret,KSYM_NAME_LEN -1 ); +strncpy (namebuf, ret, buf_size -1 ); ret = namebuf; }preempt_enable ();
@@ -350,7 +350,7 @@ const char *module_address_lookup(unsigned long addr,return ret; } -int lookup_module_symbol_name (unsigned long addr,char *symname) +int lookup_module_symbol_name (unsigned long addr,char *symname,size_t size) {struct module *mod;
@@ -365,7 +365,7 @@ int lookup_module_symbol_name(unsigned long addr, char *symname)if (!sym) goto out; -strscpy (symname, sym,KSYM_NAME_LEN ); +strscpy (symname, sym, size);preempt_enable ();return 0 ; }
kernel/trace/ftrace.c +5 -4
@@ -520,7 +520,7 @@ static int function_stat_show(struct seq_file *m, void *v) goto out;#endif -kallsyms_lookup (rec->ip,NULL ,NULL ,NULL , str); +kallsyms_lookup (rec->ip,NULL ,NULL ,NULL , str,KSYM_SYMBOL_LEN );seq_printf (m," %-30.30s %10lu" , str, rec->counter);#ifdef CONFIG_FUNCTION_GRAPH_TRACER
@@ -3980,7 +3980,7 @@ ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,char str[KSYM_SYMBOL_LEN ];char *modname; -kallsyms_lookup (rec->ip,NULL ,NULL , &modname, str); +kallsyms_lookup (rec->ip,NULL ,NULL , &modname, str,KSYM_SYMBOL_LEN );if (mod_g) {int mod_matches = (modname) ?ftrace_match (modname, mod_g) :0 ;
@@ -4738,7 +4738,7 @@ unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,if (func_g.search) {kallsyms_lookup (entry->ip,NULL ,NULL , -NULL , str); +NULL , str,KSYM_SYMBOL_LEN );if (!ftrace_match (str, &func_g))continue ; }
@@ -6846,7 +6846,8 @@ static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,char *modname;const char *ret; - ret =kallsyms_lookup (rec->ip, &symsize, &offset, &modname, str); + ret =kallsyms_lookup (rec->ip, &symsize, &offset, &modname, + str,KSYM_SYMBOL_LEN );if (!ret)return ;
kernel/trace/trace_kprobe.c +1 -1
@@ -456,7 +456,7 @@ static bool within_notrace_func(struct trace_kprobe *tk)return false;/* Check if the address is on a suffixed-symbol */ -if (!lookup_symbol_name (addr, symname)) { +if (!lookup_symbol_name (addr, symname,KSYM_NAME_LEN )) { p =strchr (symname,'.' );if (!p)return true;
kernel/trace/trace_output.c +1 -1
@@ -364,7 +364,7 @@ trace_seq_print_sym(struct trace_seq *s, unsigned long address, bool offset)if (offset)sprint_symbol (str,KSYM_SYMBOL_LEN , address);else -kallsyms_lookup (address,NULL ,NULL ,NULL , str); +kallsyms_lookup (address,NULL ,NULL ,NULL , str,KSYM_SYMBOL_LEN ); name =kretprobed (str, address);if (name &&strlen (name)) {
kernel/trace/trace_syscalls.c +1 -1
@@ -89,7 +89,7 @@ find_syscall_meta(unsigned long syscall) start = __start_syscalls_metadata; stop = __stop_syscalls_metadata; -kallsyms_lookup (syscall,NULL ,NULL ,NULL , str); +kallsyms_lookup (syscall,NULL ,NULL ,NULL , str,KSYM_SYMBOL_LEN );if (arch_syscall_match_sym_name (str,"sys_ni_syscall" ))return NULL ;
Signature · 1 line
2.17.1
PATCH 5/5 kallsyms: remove unsed API lookup_symbol_attrs
In reply to Maninder Singhwith commit '7878c231dae0 ("slab: remove /proc/slab_allocators")' lookup_symbol_attrs usage is removed.
Thus removing redundant API.
--- include/linux/kallsyms.h | 6 ------ include/linux/module.h | 6 ------ kernel/kallsyms.c | 28 ---------------------------- kernel/module/kallsyms.c | 28 ---------------------------- 4 files changed, 68 deletions(-)
include/linux/kallsyms.h +0 -6
@@ -91,7 +91,6 @@ extern int sprint_backtrace(char *buffer, size_t size, unsigned long address);extern int sprint_backtrace_build_id (char *buffer,size_t size,unsigned long address);int lookup_symbol_name (unsigned long addr,char *symname,size_t size); -int lookup_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name);/* How and when do we show kallsyms values? */ extern bool kallsyms_show_value (const struct cred *cred);
@@ -153,11 +152,6 @@ static inline int lookup_symbol_name(unsigned long addr, char *symname, size_t sreturn -ERANGE ; } -static inline int lookup_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name) -{ -return -ERANGE ; -} -static inline bool kallsyms_show_value (const struct cred *cred) {return false;
include/linux/module.h +0 -6
@@ -658,7 +658,6 @@ const char *module_address_lookup(unsigned long addr, char **modname, const unsigned char **modbuildid, char *namebuf, size_t buf_size);int lookup_module_symbol_name (unsigned long addr,char *symname,size_t size); -int lookup_module_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name);int register_module_notifier (struct notifier_block *nb);int unregister_module_notifier (struct notifier_block *nb);
@@ -766,11 +765,6 @@ static inline int lookup_module_symbol_name(unsigned long addr, char *symname, sreturn -ERANGE ; } -static inline int lookup_module_symbol_attrs (unsigned long addr,unsigned long *size,unsigned long *offset,char *modname,char *name) -{ -return -ERANGE ; -} -static inline int module_get_kallsym (unsigned int symnum,unsigned long *value,char *type,char *name,char *module_name,int *exported)
kernel/kallsyms.c +0 -28
@@ -430,34 +430,6 @@ int lookup_symbol_name(unsigned long addr, char *symname, size_t size)return 0 ; } -int lookup_symbol_attrs (unsigned long addr,unsigned long *size, -unsigned long *offset,char *modname,char *name) -{ -int res; - - name[0 ] ='\0' ; - name[KSYM_NAME_LEN -1 ] ='\0' ; - -if (is_ksym_addr (addr)) { -unsigned long pos; - - pos =get_symbol_pos (addr, size, offset); -/* Grab name */ -kallsyms_expand_symbol (get_symbol_offset (pos), - name,KSYM_NAME_LEN ); - modname[0 ] ='\0' ; - goto found; - } -/* See if it's in a module. */ - res =lookup_module_symbol_attrs (addr, size, offset, modname, name); -if (res) -return res; - -found: -cleanup_symbol_name (name); -return 0 ; -} -/* Look up a kernel symbol and return it in a text buffer. */ static int __sprint_symbol (char *buffer,size_t buf_size,unsigned long address,int symbol_offset,int add_offset,int add_buildid)
kernel/module/kallsyms.c +0 -28
@@ -375,34 +375,6 @@ int lookup_module_symbol_name(unsigned long addr, char *symname, size_t size)return -ERANGE ; } -int lookup_module_symbol_attrs (unsigned long addr,unsigned long *size, -unsigned long *offset,char *modname,char *name) -{ -struct module *mod; - -preempt_disable (); -list_for_each_entry_rcu (mod, &modules, list) { -if (mod->state ==MODULE_STATE_UNFORMED ) -continue ; -if (within_module (addr, mod)) { -const char *sym; - - sym =find_kallsyms_symbol (mod, addr, size, offset); -if (!sym) - goto out; -if (modname) -strscpy (modname, mod->name,MODULE_NAME_LEN ); -if (name) -strscpy (name, sym,KSYM_NAME_LEN ); -preempt_enable (); -return 0 ; - } - } -out: -preempt_enable (); -return -ERANGE ; -} -int module_get_kallsym (unsigned int symnum,unsigned long *value,char *type,char *name,char *module_name,int *exported) {
Signature · 1 line
2.17.1
Quoted 7 lines · Maninder Singh
kallsyms functionality depends on
KSYM_NAME_LENdirectly. but if user passed array length lesser than it, sprintf can cause issues of buffer overflow attack.So changing *sprint* and *lookup* APIs in this patch set to have buffer size as an argument and replacing sprintf with scnprintf.
This is still a pretty horrible API. Passing something like a struct seq_buf seems like the much better API here. Also with the amount of arguments and by reference passing it might be worth to pass them as a structure while you're at it.
Quoted 5 lines · Christoph Hellwig
Quoted 4 lines · Maninder Singh
kallsyms functionality depends on
KSYM_NAME_LENdirectly. but if user passed array length lesser than it, sprintf can cause issues of buffer overflow attack.
Quoted 8 lines
Quoted 3 lines
So changing *sprint* and *lookup* APIs in this patch set to have buffer size as an argument and replacing sprintf with scnprintf.
This is still a pretty horrible API. Passing something like a struct
seq_bufseems like the much better API here. Also with the amount of arguments and by reference passing it might be worth to pass them as a structure while you're at it.
Yeah, I agree. It really seems like seq_buf would be nicer.
Signature · 1 line
Kees Cook
Petr Mladek
maintainerQuoted 10 lines · Kees Cook
Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Maninder Singh
kallsyms functionality depends on
KSYM_NAME_LENdirectly. but if user passed array length lesser than it, sprintf can cause issues of buffer overflow attack.So changing *sprint* and *lookup* APIs in this patch set to have buffer size as an argument and replacing sprintf with scnprintf.
Quoted 6 lines
Quoted 4 lines
This is still a pretty horrible API. Passing something like a struct
seq_bufseems like the much better API here. Also with the amount of arguments and by reference passing it might be worth to pass them as a structure while you're at it.Yeah, I agree. It really seems like
seq_bufwould be nicer.
There is a new patchset that is trying to use this kind of buffer in vsprintf.
It introduces another buffer struct because vsprintf() needs a bit different semantic than the one used in seq_buf. But it actually replaces seq_buf() in the end. I am not sure if this is the right approach.
Anyway, the initial API is very simple, see
https://lore.kernel.org/r/20220604193042.1674951-2-kent.overstreet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
And it makes the internal vsprintf() API more sane, see
https://lore.kernel.org/r/20220604193042.1674951-4-kent.overstreet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
It would eventually solve also concerns about the kallsysms API. Any comments on the new printbuf API are much appreaciated.
Best Regards,
Petr