kprobes: be more permissive when user specifies both symbol name and address
Subsystems (2)
PATCH 1/1 kprobes: be more permissive when user specifies both symbol name and address
Currently, if user specifies both symbol name and address, we just bail out.
This might be too rude. This patch makes it give more tolerance. If both are specified, check address first, if the symbol found does not match the one user specify, print a waring. If not found, return -ENOENT, because some symbols might have muplitple instances, we don't bother to check symbol name.
--- Documentation/kprobes.txt | 4 +++- kernel/kprobes.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-)
Documentation/kprobes.txt +3 -1
@@ -344,7 +344,9 @@ to install a probepoint is known. This field is used to calculate the probepoint. 3. Specify either the kprobe "symbol_name" OR the "addr". If both are -specified, kprobe registration will fail with -EINVAL. +specified, only check "addr", because some symbols might have muplitple +instances. If neither is specified, kprobe registration will fail +with -EINVAL. 4. With CISC architectures (such as i386 and x86_64), the kprobes code does not validate if the kprobe.addr is at an instruction boundary.
kernel/kprobes.c +26 -4
@@ -1354,17 +1354,39 @@ static int __kprobes in_kprobes_functions(unsigned long addr)static kprobe_opcode_t __kprobes *kprobe_addr (struct kprobe *p) {kprobe_opcode_t *addr = p->addr; +char namebuf[KSYM_NAME_LEN ]; +const char *sym_name =NULL ; +unsigned long offset; -if ((p->symbol_name && p->addr) || - (!p->symbol_name && !p->addr)) +if (!p->symbol_name && !p->addr) goto invalid; -if (p->symbol_name) { +/* Some symbols might have muplitple instances, +* so if both specified, only check address. */ +if (unlikely (p->addr && p->symbol_name)) { + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT ); + +if (strncmp (sym_name, p->symbol_name,KSYM_NAME_LEN ) + || offset != p->offset) { +pr_err ("Incorrect symbol or offset, should be " +"symbol=%s, offset=%ld.\n" , sym_name, offset); + goto invalid; + } + }else if (p->symbol_name) { +/* only symbol case */ kprobe_lookup_name (p->symbol_name, addr);if (!addr)return ERR_PTR (-ENOENT ); + }else { +/* only address case */ + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT ); } - addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);if (addr)return addr;
Signature · 1 line
1.9.0.GIT
Quoted 40 lines · Jianyu Zhan
Currently, if user specifies both symbol name and address, we just bail out.
This might be too rude. This patch makes it give more tolerance. If both are specified, check address first, if the symbol found does not match the one user specify, print a waring. If not found, return
-ENOENT, because some symbols might have muplitple instances, we don't bother to check symbol name.Signed-off-by Jianyu Zhan <[email protected]>--- Documentation/kprobes.txt | 4 +++- kernel/kprobes.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-)Documentation/kprobes.txt +3
-1@@ -344,7 +344,9 @@ to install a probepoint is known. This field is used to calculate the probepoint. 3. Specify either the kprobe "symbol_name" OR the "addr". If both are -specified, kprobe registration will fail with -EINVAL. +specified, only check "addr", because some symbols might have muplitple +instances. If neither is specified, kprobe registration will fail +with -EINVAL. 4. With CISC architectures (such as i386 and x86_64), the kprobes code does not validate if the kprobe.addr is at an instruction boundary.kernel/kprobes.c +2
-0@@ -1354,17 +1354,39 @@ static int __kprobes in_kprobes_functions(unsigned long addr)static kprobe_opcode_t __kprobes *kprobe_addr (struct kprobe *p) {kprobe_opcode_t *addr = p->addr; +char namebuf[KSYM_NAME_LEN ]; +const char *sym_name =NULL ;
kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1365
Quoted 10 lines
kernel/kprobes.c +4
-3+unsigned long offset; -if ((p->symbol_name && p->addr) || - (!p->symbol_name && !p->addr)) +if (!p->symbol_name && !p->addr) goto invalid; -if (p->symbol_name) { +/* Some symbols might have muplitple instances, +* so if both specified, only check address. */
Could you fix the comment style as same as others? If we have multiple lines of comment, it should be
/*
* aaaaaa
* bbbbbb
*/
Quoted 13 lines
kernel/kprobes.c +13
-0+if (unlikely (p->addr && p->symbol_name)) { + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT ); + +if (strncmp (sym_name, p->symbol_name,KSYM_NAME_LEN ) + || offset != p->offset) { +pr_err ("Incorrect symbol or offset, should be " +"symbol=%s, offset=%ld.\n" , sym_name, offset); + goto invalid; + } + }else if (p->symbol_name) {
kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1388
Quoted 10 lines
kernel/kprobes.c +7
-0+/* only symbol case */ kprobe_lookup_name (p->symbol_name, addr);if (!addr)return ERR_PTR (-ENOENT ); + }else { +/* only address case */ + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT );
Since we've already have a sanity check of the address range (in kernel_text) in check_kprobe_address_safe(), you don't need to lookup kallsyms.
Thank you,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]
Sorry for resending...
Quoted 40 lines · Jianyu Zhan
Currently, if user specifies both symbol name and address, we just bail out.
This might be too rude. This patch makes it give more tolerance. If both are specified, check address first, if the symbol found does not match the one user specify, print a waring. If not found, return
-ENOENT, because some symbols might have muplitple instances, we don't bother to check symbol name.Signed-off-by Jianyu Zhan <[email protected]>--- Documentation/kprobes.txt | 4 +++- kernel/kprobes.c | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-)Documentation/kprobes.txt +3
-1@@ -344,7 +344,9 @@ to install a probepoint is known. This field is used to calculate the probepoint. 3. Specify either the kprobe "symbol_name" OR the "addr". If both are -specified, kprobe registration will fail with -EINVAL. +specified, only check "addr", because some symbols might have muplitple +instances. If neither is specified, kprobe registration will fail +with -EINVAL. 4. With CISC architectures (such as i386 and x86_64), the kprobes code does not validate if the kprobe.addr is at an instruction boundary.kernel/kprobes.c +2
-0@@ -1354,17 +1354,39 @@ static int __kprobes in_kprobes_functions(unsigned long addr)static kprobe_opcode_t __kprobes *kprobe_addr (struct kprobe *p) {kprobe_opcode_t *addr = p->addr; +char namebuf[KSYM_NAME_LEN ]; +const char *sym_name =NULL ;
kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1365
Quoted 10 lines
kernel/kprobes.c +4
-3+unsigned long offset; -if ((p->symbol_name && p->addr) || - (!p->symbol_name && !p->addr)) +if (!p->symbol_name && !p->addr) goto invalid; -if (p->symbol_name) { +/* Some symbols might have muplitple instances, +* so if both specified, only check address. */
Could you fix the comment style as same as others? If we have multiple lines of comment, it should be
/*
* aaaaaa
* bbbbbb
*/
Quoted 13 lines
kernel/kprobes.c +13
-0+if (unlikely (p->addr && p->symbol_name)) { + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT ); + +if (strncmp (sym_name, p->symbol_name,KSYM_NAME_LEN ) + || offset != p->offset) { +pr_err ("Incorrect symbol or offset, should be " +"symbol=%s, offset=%ld.\n" , sym_name, offset); + goto invalid; + } + }else if (p->symbol_name) {
kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1388
Quoted 10 lines
kernel/kprobes.c +7
-0+/* only symbol case */ kprobe_lookup_name (p->symbol_name, addr);if (!addr)return ERR_PTR (-ENOENT ); + }else { +/* only address case */ + sym_name =kallsyms_lookup ((unsigned long )(p->addr), +NULL , &offset,NULL , namebuf); +if (!sym_name) +return ERR_PTR (-ENOENT );
Since we've already have a sanity check of the address range (in kernel_text) in check_kprobe_address_safe(), you don't need to lookup kallsyms.
Thank you,
Signature · 4 lines
Masami HIRAMATSU Software Platform Research Dept. Linux Technology Center Hitachi, Ltd., Yokohama Research Laboratory E-mail: [email protected]