Skip to contentSkip to conversation

kprobes: be more permissive when user specifies both symbol name and address

PATCH In review Jianyu Zhan 2 participants 6 messages Apr 14, 2014 – Apr 15, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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, let symbol name take precedence; upon failure, try address. And print a warning message if user specify an address to inform him that using symbol is more preferred.

---
 Documentation/kprobes.txt |  4 +++-
 kernel/kprobes.c          | 33 +++++++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)
Documentation/kprobes.txt +3 -1
index 0cfb00f..ecf901b 100644
@@ -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, searching for "symbol_name" takes precedence; upon failure,
+then try "addr". 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 +29 -4
index ceeadfc..2444a7a 100644
@@ -1354,17 +1354,42 @@ 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;
 
-	if ((p->symbol_name && p->addr) ||
-	    (!p->symbol_name && !p->addr))
+	if (!p->symbol_name && !p->addr)
 		goto invalid;
 
 	if (p->symbol_name) {
 		kprobe_lookup_name(p->symbol_name, addr);
-		if (!addr)
-			return ERR_PTR(-ENOENT);
+		if (addr) {
+			if (p->addr && p->addr != addr)
+				printk(KERN_WARNING "Warning: kprobe adrress for %s "
+					"mismatch, should be %p, not %p.\n",
+					p->symbol_name, addr, p->addr);
+			goto found;
+		}
+	}
+	if (p->addr) {
+		printk(KERN_WARNING "Warning: kprobes symbol name is now supported."
+			"Please use it instead.\n");
+		sym_name = kallsyms_lookup((unsigned long)(p->addr),
+				NULL, NULL, NULL, namebuf);
+		if (sym_name) {
+			if (p->symbol_name && strncmp(sym_name, p->symbol_name,
+						KSYM_NAME_LEN))
+				printk(KERN_WARNING "Warning: found %s at addres "
+				"%p, but not %s.\n",
+				p->symbol_name, addr, sym_name);
+
+			addr = p->addr;
+			goto found;
+		}
+
 	}
+	return ERR_PTR(-ENOENT);
 
+found:
 	addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);
 	if (addr)
 		return addr;
Signature · 1 line
1.9.0.GIT
#1 lore raw
Quoted 7 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, let symbol name take precedence; upon failure, try address. And print a warning message if user specify an address to inform him that using symbol is more preferred.

No, please don't do such thing. Using addr and symbol for double checking is good, but if user sets "func1" and if it is not found, it should be an error.
This is for the fail-safe and foolproof principle.
See below comments too.

Quoted 38 lines
---
 Documentation/kprobes.txt |  4 +++-
 kernel/kprobes.c          | 33 +++++++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)
Documentation/kprobes.txt +3 -1
index 0cfb00f..ecf901b 100644
@@ -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, searching for "symbol_name" takes precedence; upon failure,
+then try "addr". 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 +3 -2
index ceeadfc..2444a7a 100644
@@ -1354,17 +1354,42 @@ 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;
 
-	if ((p->symbol_name && p->addr) ||
-	    (!p->symbol_name && !p->addr))
+	if (!p->symbol_name && !p->addr)
 		goto invalid;
 
 	if (p->symbol_name) {

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1371

Quoted 10 lines
kernel/kprobes.c +7 -2
 		kprobe_lookup_name(p->symbol_name, addr);
-		if (!addr)
-			return ERR_PTR(-ENOENT);
+		if (addr) {
+			if (p->addr && p->addr != addr)
+				printk(KERN_WARNING "Warning: kprobe adrress for %s "
+					"mismatch, should be %p, not %p.\n",
+					p->symbol_name, addr, p->addr);
+			goto found;
+		}

This should be an error.

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1375

Quoted 4 lines
kernel/kprobes.c +4 -0
+	}
+	if (p->addr) {
+		printk(KERN_WARNING "Warning: kprobes symbol name is now supported."
+			"Please use it instead.\n");

No, don't put this warning. Since the local symbols can have same name in the kallsyms, sometimes p->addr is the only solution. From the same reason, if you need a double check, you should check p->addr first. sometimes kprobe_lookup_name() may return unwilling address (same symbol but another instance).

So the correct double-check should be;

----
if (p->addr) {

  if (p->symbol) {
    sym = kallsyms_lookup(p->addr, ... &offs ...);
    if (strcmp(sym,p->symbol) != 0 || offs != p->offset) {
      pr_warning("Error! ...");
      goto fail;
    }

}
} else if (p->symbol) {
kprobe_lookup_name(p->symbol_name, addr);
if (!addr)

    goto fail;
} else

goto fail;
----

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1385

Quoted 10 lines
kernel/kprobes.c +10 -0
+		sym_name = kallsyms_lookup((unsigned long)(p->addr),
+				NULL, NULL, NULL, namebuf);
+		if (sym_name) {
+			if (p->symbol_name && strncmp(sym_name, p->symbol_name,
+						KSYM_NAME_LEN))
+				printk(KERN_WARNING "Warning: found %s at addres "
+				"%p, but not %s.\n",
+				p->symbol_name, addr, sym_name);
+
+			addr = p->addr;

Here, we also treat this as an error.

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1395

Quoted 11 lines
kernel/kprobes.c +5 -0
+			goto found;
+		}
+
 	}
+	return c
 
+found:
 	addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);
 	if (addr)
 		return addr;

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#2 lore raw
Quoted 7 lines · Masami Hiramatsu

if (p->addr) {

  if (p->symbol) {
    sym = kallsyms_lookup(p->addr, ... &offs ...);
    if (strcmp(sym,p->symbol) != 0 || offs != p->offset) {
      pr_warning("Error! ...");
      goto fail;
    }
Quoted 7 lines

}
} else if (p->symbol) {
kprobe_lookup_name(p->symbol_name, addr);
if (!addr)

    goto fail;
} else

goto fail;

Hmm, let's clasify all conditions.

1. Only symbol, check it, if not found, fail.
2. Only address, check it, if not found, fail.
3. Both, check address,
3.1 not found, fail, because some symbols might have muplitple instances,
we don't bother to check symbol name.
3.2 found, check if symbol mismatch, if yes, fail.

Is this reasonable? Next mail is a renewed patch following this priciple.

Regards,
Jianyu Zhan

#3 lore raw
Quoted 18 lines · Zhan Jianyu
Quoted 7 lines · Masami Hiramatsu

if (p->addr) {

  if (p->symbol) {
    sym = kallsyms_lookup(p->addr, ... &offs ...);
    if (strcmp(sym,p->symbol) != 0 || offs != p->offset) {
      pr_warning("Error! ...");
      goto fail;
    }
Quoted 7 lines

}
} else if (p->symbol) {
kprobe_lookup_name(p->symbol_name, addr);
if (!addr)

    goto fail;
} else

goto fail;

Quoted 8 lines

Hmm, let's clasify all conditions.

1. Only symbol, check it, if not found, fail.
2. Only address, check it, if not found, fail.
3. Both, check address,
3.1 not found, fail, because some symbols might have muplitple instances,
we don't bother to check symbol name.
3.2 found, check if symbol mismatch, if yes, fail.

Plus, if the p->offset and offs are different, fail too.

Quoted 1 line

Is this reasonable? Next mail is a renewed patch following this priciple.

OK, let me see. :)

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#4 lore raw
Quoted 2 lines · Masami Hiramatsu

Plus, if the p->offset and offs are different, fail too.

Oh, yeah, I have did this check it patch too. BTW, I found that the offset has different types
in kallsyms_lookup(which is unsigned long) and in struct kprobe(which is unsigned int)

Regards,
Jianyu Zhan

#5 lore raw
In reply to Jianyu Zhan
Quoted 7 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, let symbol name take precedence; upon failure, try address. And print a warning message if user specify an address to inform him that using symbol is more preferred.

No, please don't do such thing. Using addr and symbol for double checking is good, but if user sets "func1" and if it is not found, it should be an error.
This is for the fail-safe and foolproof principle.
See below comments too.

Quoted 38 lines
---
 Documentation/kprobes.txt |  4 +++-
 kernel/kprobes.c          | 33 +++++++++++++++++++++++++++++----
 2 files changed, 32 insertions(+), 5 deletions(-)
Documentation/kprobes.txt +3 -1
index 0cfb00f..ecf901b 100644
@@ -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, searching for "symbol_name" takes precedence; upon failure,
+then try "addr". 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 +3 -2
index ceeadfc..2444a7a 100644
@@ -1354,17 +1354,42 @@ 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;
 
-	if ((p->symbol_name && p->addr) ||
-	    (!p->symbol_name && !p->addr))
+	if (!p->symbol_name && !p->addr)
 		goto invalid;
 
 	if (p->symbol_name) {

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1371

Quoted 10 lines
kernel/kprobes.c +7 -2
 		kprobe_lookup_name(p->symbol_name, addr);
-		if (!addr)
-			return ERR_PTR(-ENOENT);
+		if (addr) {
+			if (p->addr && p->addr != addr)
+				printk(KERN_WARNING "Warning: kprobe adrress for %s "
+					"mismatch, should be %p, not %p.\n",
+					p->symbol_name, addr, p->addr);
+			goto found;
+		}

This should be an error.

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1375

Quoted 4 lines
kernel/kprobes.c +4 -0
+	}
+	if (p->addr) {
+		printk(KERN_WARNING "Warning: kprobes symbol name is now supported."
+			"Please use it instead.\n");

No, don't put this warning. Since the local symbols can have same name in the kallsyms, sometimes p->addr is the only solution. From the same reason, if you need a double check, you should check p->addr first. sometimes kprobe_lookup_name() may return unwilling address (same symbol but another instance).

So the correct double-check should be;

----
if (p->addr) {

  if (p->symbol) {
    sym = kallsyms_lookup(p->addr, ... &offs ...);
    if (strcmp(sym,p->symbol) != 0 || offs != p->offset) {
      pr_warning("Error! ...");
      goto fail;
    }

}
} else if (p->symbol) {
kprobe_lookup_name(p->symbol_name, addr);
if (!addr)

    goto fail;
} else

goto fail;
----

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1385

Quoted 10 lines
kernel/kprobes.c +10 -0
+		sym_name = kallsyms_lookup((unsigned long)(p->addr),
+				NULL, NULL, NULL, namebuf);
+		if (sym_name) {
+			if (p->symbol_name && strncmp(sym_name, p->symbol_name,
+						KSYM_NAME_LEN))
+				printk(KERN_WARNING "Warning: found %s at addres "
+				"%p, but not %s.\n",
+				p->symbol_name, addr, sym_name);
+
+			addr = p->addr;

Here, we also treat this as an error.

kernel/kprobes.c static int __kprobes in_kprobes_functions(unsigned long addr) · line 1395

Quoted 11 lines
kernel/kprobes.c +5 -0
+			goto found;
+		}
+
 	}
+	return c
 
+found:
 	addr = (kprobe_opcode_t *)(((char *)addr) + p->offset);
 	if (addr)
 		return addr;

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]