Skip to content

Commit a4c5bfd

Browse files
b10902118intel-lab-lkp
authored andcommitted
arm64: ptrace: fix hw_break_set() to set addr and ctrl together
This patch fixes the failure of PTRACE_SETREGSET when setting a hardware breakpoint on a non-4-byte aligned address with a valid control to a 32-bit tracee. The issue was discovered while testing LLDB. Link: llvm/llvm-project#152284 The failure happens because hw_break_set() checks and sets the breakpoint address and control separately. This can result in an check failure when it first validates the address to be set with old control. For example, the control are initialized with breakpoint length of 4. Combining with a non-4-byte aligned address would cross a 4-byte boundary, which is invalid. However, the user-provided control may actually specify a length of 1, which should be valid. The fix is to set the address and control together. This is supported by modify_user_hw_breakpoint(), the function that sets and checks breakpoints. The original implementation wrap this function to ptrace_hbp_set_addr() and ptrace_hbp_set_ctrl() for 32-bit API PTRACE_SETHBPREGS simply because it can only modify one register (address or control) per call. For the 64-bit PTRACE_SETREGSET API, this restriction does not apply, so a new helper function ptrace_hbp_set() was added to set both in a single call to modify_user_hw_breakpoint(). Then ptrace_hbp_set_addr() and ptrace_hbp_set_ctrl() are only used by compat_ptrace_hbp_set(), so moved into CONFIG_COMPAT block and renamed with prefix compat_. For reference, the check is in arch/arm64/kernel/hw_breakpoint.c:hw_breakpoint_arch_parse() which is called via: modify_user_hw_breakpoint() -> modify_user_hw_breakpoint_check() -> hw_breakpoint_parse() -> hw_breakpoint_arch_parse() Signed-off-by: Bill Tsui <[email protected]>
1 parent f620d66 commit a4c5bfd

File tree

1 file changed

+58
-32
lines changed

1 file changed

+58
-32
lines changed

arch/arm64/kernel/ptrace.c

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,11 @@ static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
421421
return bp;
422422
}
423423

424-
static int ptrace_hbp_set_ctrl(unsigned int note_type,
425-
struct task_struct *tsk,
426-
unsigned long idx,
427-
u32 uctrl)
424+
/* Set the address and control together for non-compat ptrace */
425+
static int ptrace_hbp_set(unsigned int note_type,
426+
struct task_struct *tsk,
427+
unsigned long idx,
428+
u64 addr, u32 uctrl)
428429
{
429430
int err;
430431
struct perf_event *bp;
@@ -438,6 +439,8 @@ static int ptrace_hbp_set_ctrl(unsigned int note_type,
438439
}
439440

440441
attr = bp->attr;
442+
attr.bp_addr = addr;
443+
441444
decode_ctrl_reg(uctrl, &ctrl);
442445
err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
443446
if (err)
@@ -446,27 +449,6 @@ static int ptrace_hbp_set_ctrl(unsigned int note_type,
446449
return modify_user_hw_breakpoint(bp, &attr);
447450
}
448451

449-
static int ptrace_hbp_set_addr(unsigned int note_type,
450-
struct task_struct *tsk,
451-
unsigned long idx,
452-
u64 addr)
453-
{
454-
int err;
455-
struct perf_event *bp;
456-
struct perf_event_attr attr;
457-
458-
bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
459-
if (IS_ERR(bp)) {
460-
err = PTR_ERR(bp);
461-
return err;
462-
}
463-
464-
attr = bp->attr;
465-
attr.bp_addr = addr;
466-
err = modify_user_hw_breakpoint(bp, &attr);
467-
return err;
468-
}
469-
470452
#define PTRACE_HBP_ADDR_SZ sizeof(u64)
471453
#define PTRACE_HBP_CTRL_SZ sizeof(u32)
472454
#define PTRACE_HBP_PAD_SZ sizeof(u32)
@@ -524,9 +506,6 @@ static int hw_break_set(struct task_struct *target,
524506
return -EINVAL;
525507
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
526508
offset, offset + PTRACE_HBP_ADDR_SZ);
527-
if (ret)
528-
return ret;
529-
ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
530509
if (ret)
531510
return ret;
532511
offset += PTRACE_HBP_ADDR_SZ;
@@ -537,10 +516,11 @@ static int hw_break_set(struct task_struct *target,
537516
offset, offset + PTRACE_HBP_CTRL_SZ);
538517
if (ret)
539518
return ret;
540-
ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
519+
offset += PTRACE_HBP_CTRL_SZ;
520+
521+
ret = ptrace_hbp_set(note_type, target, idx, addr, ctrl);
541522
if (ret)
542523
return ret;
543-
offset += PTRACE_HBP_CTRL_SZ;
544524

545525
user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
546526
offset, offset + PTRACE_HBP_PAD_SZ);
@@ -2139,6 +2119,52 @@ static int compat_ptrace_hbp_get(unsigned int note_type,
21392119
return err;
21402120
}
21412121

2122+
static int compat_ptrace_hbp_set_ctrl(unsigned int note_type,
2123+
struct task_struct *tsk,
2124+
unsigned long idx,
2125+
u32 uctrl)
2126+
{
2127+
int err;
2128+
struct perf_event *bp;
2129+
struct perf_event_attr attr;
2130+
struct arch_hw_breakpoint_ctrl ctrl;
2131+
2132+
bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
2133+
if (IS_ERR(bp)) {
2134+
err = PTR_ERR(bp);
2135+
return err;
2136+
}
2137+
2138+
attr = bp->attr;
2139+
decode_ctrl_reg(uctrl, &ctrl);
2140+
err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
2141+
if (err)
2142+
return err;
2143+
2144+
return modify_user_hw_breakpoint(bp, &attr);
2145+
}
2146+
2147+
static int compat_ptrace_hbp_set_addr(unsigned int note_type,
2148+
struct task_struct *tsk,
2149+
unsigned long idx,
2150+
u64 addr)
2151+
{
2152+
int err;
2153+
struct perf_event *bp;
2154+
struct perf_event_attr attr;
2155+
2156+
bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
2157+
if (IS_ERR(bp)) {
2158+
err = PTR_ERR(bp);
2159+
return err;
2160+
}
2161+
2162+
attr = bp->attr;
2163+
attr.bp_addr = addr;
2164+
err = modify_user_hw_breakpoint(bp, &attr);
2165+
return err;
2166+
}
2167+
21422168
static int compat_ptrace_hbp_set(unsigned int note_type,
21432169
struct task_struct *tsk,
21442170
compat_long_t num,
@@ -2151,10 +2177,10 @@ static int compat_ptrace_hbp_set(unsigned int note_type,
21512177

21522178
if (num & 1) {
21532179
addr = *kdata;
2154-
err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
2180+
err = compat_ptrace_hbp_set_addr(note_type, tsk, idx, addr);
21552181
} else {
21562182
ctrl = *kdata;
2157-
err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
2183+
err = compat_ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
21582184
}
21592185

21602186
return err;

0 commit comments

Comments
 (0)