Skip to content

Commit 1deb9c5

Browse files
arch/tile: don't allow user code to set the PL via ptrace or signal return
The kernel was allowing any component of the pt_regs to be updated either by signal handlers writing to the stack, or by processes writing via PTRACE_POKEUSR or PTRACE_SETREGS, which meant they could set their PL up from 0 to 1 and get access to kernel code and data (or, in practice, cause a kernel panic). We now always reset the ex1 field, allowing the user to set their ICS bit only. Signed-off-by: Chris Metcalf <cmetcalf@tilera.com>
1 parent 34a89d2 commit 1deb9c5

2 files changed

Lines changed: 24 additions & 18 deletions

File tree

‎arch/tile/kernel/ptrace.c‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ long arch_ptrace(struct task_struct *child, long request,
5050
{
5151
unsigned long __user *datap = (long __user __force *)data;
5252
unsigned long tmp;
53-
int i;
5453
long ret = -EIO;
55-
unsigned long *childregs;
5654
char *childreg;
55+
struct pt_regs copyregs;
56+
int ex1_offset;
5757

5858
switch (request) {
5959

@@ -80,6 +80,16 @@ long arch_ptrace(struct task_struct *child, long request,
8080
if (addr >= PTREGS_SIZE)
8181
break;
8282
childreg = (char *)task_pt_regs(child) + addr;
83+
84+
/* Guard against overwrites of the privilege level. */
85+
ex1_offset = PTREGS_OFFSET_EX1;
86+
#if defined(CONFIG_COMPAT) && defined(__BIG_ENDIAN)
87+
if (is_compat_task()) /* point at low word */
88+
ex1_offset += sizeof(compat_long_t);
89+
#endif
90+
if (addr == ex1_offset)
91+
data = PL_ICS_EX1(USER_PL, EX1_ICS(data));
92+
8393
#ifdef CONFIG_COMPAT
8494
if (is_compat_task()) {
8595
if (addr & (sizeof(compat_long_t)-1))
@@ -96,26 +106,19 @@ long arch_ptrace(struct task_struct *child, long request,
96106
break;
97107

98108
case PTRACE_GETREGS: /* Get all registers from the child. */
99-
if (!access_ok(VERIFY_WRITE, datap, PTREGS_SIZE))
100-
break;
101-
childregs = (long *)task_pt_regs(child);
102-
for (i = 0; i < sizeof(struct pt_regs)/sizeof(unsigned long);
103-
++i) {
104-
ret = __put_user(childregs[i], &datap[i]);
105-
if (ret != 0)
106-
break;
109+
if (copy_to_user(datap, task_pt_regs(child),
110+
sizeof(struct pt_regs)) == 0) {
111+
ret = 0;
107112
}
108113
break;
109114

110115
case PTRACE_SETREGS: /* Set all registers in the child. */
111-
if (!access_ok(VERIFY_READ, datap, PTREGS_SIZE))
112-
break;
113-
childregs = (long *)task_pt_regs(child);
114-
for (i = 0; i < sizeof(struct pt_regs)/sizeof(unsigned long);
115-
++i) {
116-
ret = __get_user(childregs[i], &datap[i]);
117-
if (ret != 0)
118-
break;
116+
if (copy_from_user(&copyregs, datap,
117+
sizeof(struct pt_regs)) == 0) {
118+
copyregs.ex1 =
119+
PL_ICS_EX1(USER_PL, EX1_ICS(copyregs.ex1));
120+
*task_pt_regs(child) = copyregs;
121+
ret = 0;
119122
}
120123
break;
121124

‎arch/tile/kernel/signal.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ int restore_sigcontext(struct pt_regs *regs,
7171
for (i = 0; i < sizeof(struct pt_regs)/sizeof(long); ++i)
7272
err |= __get_user(regs->regs[i], &sc->gregs[i]);
7373

74+
/* Ensure that the PL is always set to USER_PL. */
75+
regs->ex1 = PL_ICS_EX1(USER_PL, EX1_ICS(regs->ex1));
76+
7477
regs->faultnum = INT_SWINT_1_SIGRETURN;
7578

7679
err |= __get_user(*pr0, &sc->gregs[0]);

0 commit comments

Comments
 (0)