Skip to contentSkip to conversation

kprobes: verify jprobe entry point

PATCH In review Namhyung Kim 6 participants 11 messages Aug 3, 2010 – Aug 5, 2010

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

PATCH 2/2 kprobes: verify jprobe entry point

verify jprobe's entry point is a function entry point using kallsyms' offset value.

kernel/kprobes.c +12 -6
index 8f96701..c7295f9 100644
@@ -1334,19 +1334,25 @@ unsigned long __weak arch_deref_entry_point(void *entry)
 int __kprobes register_jprobes(struct jprobe **jps, int num)
 {
 	struct jprobe *jp;
-	int ret = 0, i;
+	int ret = -EINVAL, i;
 
 	if (num <= 0)
-		return -EINVAL;
+		return ret;
 	for (i = 0; i < num; i++) {
 		unsigned long addr;
+		unsigned long size, offset;
+		char namebuf[KSYM_NAME_LEN];
+
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup(addr, &size, &offset, NULL, namebuf) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		}
 
 		if (ret < 0) {
 			if (i > 0)
Signature · 1 line
1.7.0.4
#1 lore raw
Quoted 14 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

kernel/kprobes.c +0 -1
index 8f96701..c7295f9 100644
@@ -1334,19 +1334,25 @@ unsigned long __weak arch_deref_entry_point(void *entry)
 int __kprobes register_jprobes(struct jprobe **jps, int num)
 {
 	struct jprobe *jp;
-	int ret = 0, i;

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1345

Quoted 10 lines
kernel/kprobes.c +5 -1
+	int ret = -EINVAL, i;
 
 	if (num <= 0)
-		return -EINVAL;
+		return ret;
 	for (i = 0; i < num; i++) {
 		unsigned long addr;
+		unsigned long size, offset;
+		char namebuf[KSYM_NAME_LEN];
+

You need to re-initialize "ret" in each iteration, or "ret" will be 0 except for the 1st iteration.

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1354

Quoted 13 lines
kernel/kprobes.c +6 -4
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup(addr, &size, &offset, NULL, namebuf) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);

because, this line assigns 0 to ret if no error.

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1355

Quoted 1 line
kernel/kprobes.c +1 -0
+		}

I think here is a good point to do that. (in else block)

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1358

Quoted 3 lines
kernel/kprobes.c +0 -0
 		if (ret < 0) {
 			if (i > 0)

Thank you,

Signature · 4 lines
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
#2 lore raw
In reply to Namhyung Kim

2010-08-03 (í™”), 20:27 +0900, Masami Hiramatsu:

Quoted 50 lines
Quoted 14 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

kernel/kprobes.c +0 -1
index 8f96701..c7295f9 100644
@@ -1334,19 +1334,25 @@ unsigned long __weak arch_deref_entry_point(void *entry)
 int __kprobes register_jprobes(struct jprobe **jps, int num)
 {
 	struct jprobe *jp;
-	int ret = 0, i;

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1345

Quoted 10 lines
kernel/kprobes.c +5 -1
+	int ret = -EINVAL, i;
 
 	if (num <= 0)
-		return -EINVAL;
+		return ret;
 	for (i = 0; i < num; i++) {
 		unsigned long addr;
+		unsigned long size, offset;
+		char namebuf[KSYM_NAME_LEN];
+

You need to re-initialize "ret" in each iteration, or "ret" will be 0 except for the 1st iteration.

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1354

Quoted 13 lines
kernel/kprobes.c +6 -4
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup(addr, &size, &offset, NULL, namebuf) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);

because, this line assigns 0 to ret if no error.

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1355

Quoted 1 line
kernel/kprobes.c +1 -0
+		}

I think here is a good point to do that. (in else block)

Quoted 1 line
Quoted 4 lines

kernel/kprobes.c unsigned long __weak arch_deref_entry_point(void *entry) · line 1358

Quoted 2 lines
kernel/kprobes.c +0 -0
 		if (ret < 0) {
 			if (i > 0)

Thank you,

Thanks for reviewing.
I'll resend the fix soon after.

Signature · 8 lines
Regards,
Namhyung Kim


--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

PATCH 2/2 kprobes: verify jprobe entry point

In reply to Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

kernel/kprobes.c +11 -4
index 8f96701..0032868 100644
@@ -1340,13 +1340,20 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
 		unsigned long addr;
+		unsigned long size, offset;
+		char namebuf[KSYM_NAME_LEN];
+
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup(addr, &size, &offset, NULL, namebuf) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)
Signature · 1 line
1.7.0.4
#4 lore raw
In reply to Namhyung Kim
Quoted 15 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

kernel/kprobes.c +2 -0
index 8f96701..0032868 100644
@@ -1340,13 +1340,20 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
 		unsigned long addr;
+		unsigned long size, offset;
+		char namebuf[KSYM_NAME_LEN];

kernel/kprobes.c int __kprobes register_jprobes(struct jprobe **jps, int num) · line 1350

Quoted 10 lines
kernel/kprobes.c +3 -4
+
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup(addr, &size, &offset, NULL, namebuf) &&

Hm, it seems that current kernel has kallsyms_lookup_size_offset() which doesn't requires namebuf, and you can ommit size if you don't need that. It will simplify this patch:)

Other parts are OK for me.

Thank you,

Quoted 9 lines
kernel/kprobes.c +6 -0
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)
Signature · 4 lines
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
#5 lore raw

PATCH 2/2 kprobes: verify jprobe entry point

In reply to Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

---

Here comes 3rd revision of the patch. Thanks to Masami Hiramatus for suggesting kallsyms_lookup_size_offset. :-)

kernel/kprobes.c +9 -5
index 8f96701..1b0dbe0 100644
@@ -1339,14 +1339,18 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 	if (num <= 0)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
-		unsigned long addr;
+		unsigned long addr, offset;
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)

--
1.7.0.4

Acked-by on v1 2/2
#6 lore raw
In reply to Namhyung Kim
Quoted 4 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

Looks good for me :) Thanks!

Quoted 36 lines

---

Here comes 3rd revision of the patch. Thanks to Masami Hiramatus for suggesting kallsyms_lookup_size_offset. :-)

kernel/kprobes.c +9 -6
index 8f96701..1b0dbe0 100644
@@ -1339,14 +1339,18 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 	if (num <= 0)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
-		unsigned long addr;
+		unsigned long addr, offset;
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */
+		if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)
--

1.7.0.4

Signature · 4 lines
Masami HIRAMATSU
2nd Research Dept.
Hitachi, Ltd., Systems Development Laboratory
E-mail: [email protected]
#7 lore raw
In reply to Namhyung Kim
Quoted 28 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

---

Here comes 3rd revision of the patch. Thanks to Masami Hiramatus for suggesting kallsyms_lookup_size_offset. :-)

kernel/kprobes.c +2 -5
index 8f96701..1b0dbe0 100644
@@ -1339,14 +1339,18 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 	if (num <= 0)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
-		unsigned long addr;
+		unsigned long addr, offset;
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */

kernel/kprobes.c int __kprobes register_jprobes(struct jprobe **jps, int num) · line 1356

Quoted 10 lines
kernel/kprobes.c +7 -0
+		if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)

I am a bit converned about this code here:

	if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&  offset = 0)

offset is set inside kallsyms_lookup_size_offset(), right ?
but can you guarantee that the calling sequence is the same on all processors ? (it is not in case of functions).

perhaps it is more secure (and readable) to do:

ret=kallsyms_lookup_size_offset(addr, NULL, &offset); if ( ret && offset = 0 )

just my 2 cents,
re,
wh

#8 lore raw
In reply to Namhyung Kim
Quoted 44 lines · walter harms
Quoted 28 lines · Namhyung Kim

verify jprobe's entry point is a function entry point using kallsyms' offset value.

---

Here comes 3rd revision of the patch. Thanks to Masami Hiramatus for suggesting kallsyms_lookup_size_offset. :-)

kernel/kprobes.c +2 -5
index 8f96701..1b0dbe0 100644
@@ -1339,14 +1339,18 @@ int __kprobes register_jprobes(struct jprobe **jps, int num)
 	if (num <= 0)
 		return -EINVAL;
 	for (i = 0; i < num; i++) {
-		unsigned long addr;
+		unsigned long addr, offset;
 		jp = jps[i];
 		addr = arch_deref_entry_point(jp->entry);
 
-		/* Todo: Verify probepoint is a function entry point */
-		jp->kp.pre_handler = setjmp_pre_handler;
-		jp->kp.break_handler = longjmp_break_handler;
-		ret = register_kprobe(&jp->kp);
+		/* Verify probepoint is a function entry point */

kernel/kprobes.c int __kprobes register_jprobes(struct jprobe **jps, int num) · line 1356

Quoted 10 lines
kernel/kprobes.c +7 -0
+		if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&
+		    offset = 0) {
+			jp->kp.pre_handler = setjmp_pre_handler;
+			jp->kp.break_handler = longjmp_break_handler;
+			ret = register_kprobe(&jp->kp);
+		} else
+			ret = -EINVAL;
 
 		if (ret < 0) {
 			if (i > 0)

I am a bit converned about this code here:

Quoted 5 lines
	if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&  offset = 0)

offset is set inside kallsyms_lookup_size_offset(), right ?
but can you guarantee that the calling sequence is the same on all processors ? (it is not in case of functions).

&& should always be evaluated lazily from left to right. Otherwise x != NULL && x->y = z wouldn't be safe.

julia

Quoted 13 lines

perhaps it is more secure (and readable) to do:

ret=kallsyms_lookup_size_offset(addr, NULL, &offset); if ( ret && offset = 0 )

just my 2 cents,
re,
wh
--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw
In reply to Namhyung Kim
Quoted 8 lines · walter harms

I am a bit converned about this code here:

	if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&  offset = 0)

offset is set inside kallsyms_lookup_size_offset(), right ?
but can you guarantee that the calling sequence is the same on all processors ? (it is not in case of functions).

Namhyung Kim's version is fine.

regards,
dan carpenter

#10 lore raw
In reply to Namhyung Kim
Quoted 11 lines · Julia Lawall
Quoted 6 lines · walter harms

I am a bit converned about this code here:
      if (kallsyms_lookup_size_offset(addr, NULL, &offset) &&  offset = 0)

offset is set inside kallsyms_lookup_size_offset(), right ?
but can you guarantee that the calling sequence is the same on all processors ? (it is  not in case of functions).

&& should always be evaluated lazily from left to right.  Otherwise x != NULL && x->y = z wouldn't be safe.

The && operator triggers a sequence point.

I do not have access to the standard (ISO/IEC 9899:1990) any longer, so the following is from a draft (but in case the text changed, there are no functional differences for this):

2.1.2.3 Program execution
...
At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.
...
3.3.13 Logical AND operator
...
Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand.

BR Håkon Løvdal