uprobes: Revert ref_ctr_offset in uprobe_unregister error path
Subsystems (3)
Jiri Olsa
reviewerPATCH 1/1 uprobes: Revert ref_ctr_offset in uprobe_unregister error path
From: Jiri Olsa <[email protected]>
There's error path that could lead to inactive uprobe:
1) uprobe_register succeeds - updates instruction to int3 and changes ref_ctr from 0 to 1
2) uprobe_unregister fails - int3 stays in place, but ref_ctr is changed to 0 (it's not restored to 1 in the fail path) uprobe is leaked
3) another uprobe_register comes and re-uses the leaked uprobe and succeds - but int3 is already in place, so ref_ctr update is skipped and it stays 0 - uprobe CAN NOT be triggered now
4) uprobe_unregister fails because ref_ctr value is unexpected
Fixing this by reverting the updated ref_ctr value back to 1 in step 2), which is the case when uprobe_unregister fails (int3 stays in place), but we have already updated refctr.
The new scenario will go as follows:
1) uprobe_register succeeds - updates instruction to int3 and changes ref_ctr from 0 to 1
2) uprobe_unregister fails - int3 stays in place and ref_ctr is reverted to 1.. uprobe is leaked
3) another uprobe_register comes and re-uses the leaked uprobe and succeds - but int3 is already in place, so ref_ctr update is skipped and it stays 1 - uprobe CAN be triggered now
4) uprobe_unregister succeeds
---
Please note it's based on mm-stable branch, because it has the latest uprobe_write_opcode rewrite changes.
kernel/events/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +2 -2
@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );/* try collapse pmd for compound page */ if (ret >0 )
Signature · 1 line
2.49.0
Quoted 2 lines · Jiri Olsa
From: Jiri Olsa <[email protected]>
Thanks for debugging.
Quoted 27 lines
There's error path that could lead to inactive uprobe:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place, butref_ctris changed to 0 (it's not restored to 1 in the fail path) uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 0 - uprobe CAN NOT be triggered now
4)uprobe_unregisterfails becauseref_ctrvalue is unexpectedFixing this by reverting the updated
ref_ctrvalue back to 1 in step 2), which is the case whenuprobe_unregisterfails (int3 stays in place), but we have already updated refctr.The new scenario will go as follows:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place andref_ctris reverted to 1.. uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 1 - uprobe CAN be triggered now
4)uprobe_unregistersucceeds
Quoted 1 line
Suggested-by Oleg Nesterov <[email protected]>
If it's in mm-stable, we should have
here
Quoted 13 lines
Signed-off-by Jiri Olsa <[email protected]>---
Please note it's based on mm-stable branch, because it has the latestuprobe_write_opcoderewrite changes.kernel/events/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/uprobes.c +0
-0
kernel/events/uprobes.c int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 585
Quoted 7 lines
kernel/events/uprobes.c +2
-2@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );
Hm, but my patch essentially did here
/* Revert back reference counter if instruction update failed. */
- if (ret && is_register && ref_ctr_updated)
+ if (ret < 0 && is_register && ref_ctr_updated)
update_ref_ctr(uprobe, mm, -1);So how come this wasn't a problem before?
Signature · 3 lines
Cheers, David / dhildenb
Quoted 70 lines · David Hildenbrand
Quoted 2 lines · Jiri Olsa
From: Jiri Olsa <[email protected]>
Thanks for debugging.
Quoted 27 lines
There's error path that could lead to inactive uprobe:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place, butref_ctris changed to 0 (it's not restored to 1 in the fail path) uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 0 - uprobe CAN NOT be triggered now
4)uprobe_unregisterfails becauseref_ctrvalue is unexpectedFixing this by reverting the updated
ref_ctrvalue back to 1 in step 2), which is the case whenuprobe_unregisterfails (int3 stays in place), but we have already updated refctr.The new scenario will go as follows:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place andref_ctris reverted to 1.. uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 1 - uprobe CAN be triggered now
4)uprobe_unregistersucceedsQuoted 1 line
Suggested-by Oleg Nesterov <[email protected]>If it's in mm-stable, we should have
Fixes ...here
Quoted 13 lines
Signed-off-by Jiri Olsa <[email protected]>---
Please note it's based on mm-stable branch, because it has the latestuprobe_write_opcoderewrite changes.kernel/events/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/uprobes.c +0
-0kernel/events/uprobes.c int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 585
Quoted 7 lines
kernel/events/uprobes.c +2
-2@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );Hm, but my patch essentially did here
/* Revert back reference counter if instruction update failed. */(quoted diff) +1
-1- if (ret && is_register && ref_ctr_updated) + if (ret < 0 && is_register && ref_ctr_updated) update_ref_ctr(uprobe, mm, -1);
Quoted 1 line
So how come this wasn't a problem before?
Oh, or was this a problem before? Then we should find the corresponding commit that needs fixing.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 34 lines · David Hildenbrand
Quoted 33 lines · David Hildenbrand
Quoted 2 lines · Jiri Olsa
From: Jiri Olsa <[email protected]>
Thanks for debugging.
Quoted 27 lines
There's error path that could lead to inactive uprobe:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place, butref_ctris changed to 0 (it's not restored to 1 in the fail path) uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 0 - uprobe CAN NOT be triggered now
4)uprobe_unregisterfails becauseref_ctrvalue is unexpectedFixing this by reverting the updated
ref_ctrvalue back to 1 in step 2), which is the case whenuprobe_unregisterfails (int3 stays in place), but we have already updated refctr.The new scenario will go as follows:
1)
uprobe_registersucceeds - updates instruction to int3 and changesref_ctrfrom 0 to 1
2)uprobe_unregisterfails - int3 stays in place andref_ctris reverted to 1.. uprobe is leaked
3) anotheruprobe_registercomes and re-uses the leaked uprobe and succeds - but int3 is already in place, soref_ctrupdate is skipped and it stays 1 - uprobe CAN be triggered now
4)uprobe_unregistersucceeds
Quoted 5 lines
Quoted 5 lines
Quoted 1 line
Suggested-by Oleg Nesterov <[email protected]>If it's in mm-stable, we should have
Fixes ...
ok
Quoted 31 lines
Quoted 31 lines
here
Quoted 13 lines
Signed-off-by Jiri Olsa <[email protected]>---
Please note it's based on mm-stable branch, because it has the latestuprobe_write_opcoderewrite changes.kernel/events/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)kernel/events/uprobes.c +0
-0kernel/events/uprobes.c int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 584
Quoted 6 lines
kernel/events/uprobes.c +2
-2@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );Hm, but my patch essentially did here
/* Revert back reference counter if instruction update failed. */(quoted diff) +1
-1- if (ret && is_register && ref_ctr_updated) + if (ret < 0 && is_register && ref_ctr_updated) update_ref_ctr(uprobe, mm, -1);
Quoted 4 lines
Quoted 1 line
So how come this wasn't a problem before?
Oh, or was this a problem before? Then we should find the corresponding commit that needs fixing.
yes, I think it was a problem before, introduced early on by:
1cc33161a83d uprobes: Support SDT markers having reference count (semaphore)
it seems the scenario described in changelog will hit the same issue even without your patch, I'll re-run the test to be sure
thanks,
jirka
kernel/events/uprobes.c int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 585
Quoted 11 lines · Jiri Olsa
kernel/events/uprobes.c +2
-2@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );
And just in case, I agree this has nothing to do with the recent changes from David.
Oleg.
David Hildenbrand
Acked-byQuoted 15 lines · Oleg Nesterov
kernel/events/uprobes.c int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, · line 585
Quoted 11 lines · Jiri Olsa
kernel/events/uprobes.c +2
-2@@ -581,8 +581,8 @@ int uprobe_write_opcode(struct arch_uprobe *auprobe, struct vm_area_struct *vma, out:/* Revert back reference counter if instruction update failed. */ -if (ret <0 && is_register && ref_ctr_updated) -update_ref_ctr (uprobe, mm,-1 ); +if (ret <0 && ref_ctr_updated) +update_ref_ctr (uprobe, mm, is_register ?-1 :1 );Acked-by Oleg Nesterov <[email protected]>
Quoted 1 line
And just in case, I agree this has nothing to do with the recent changes from David.
BTW, I stumbled over this when doing the rework.
Back then, I was wondering if this is to handle the case where un-registering effectively fails because someone MADV_DONTNEED'ed the page.
But, we only perform the update_ref_ctr() after verify_opcode(), so that does not apply.
With proper Fixes:
Signature · 3 lines
Cheers, David / dhildenb