hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/kernel/ptrace.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/kernel/ptrace.c
34 *
....@@ -30,6 +31,8 @@
3031 #include <linux/cn_proc.h>
3132 #include <linux/compat.h>
3233 #include <linux/sched/signal.h>
34
+
35
+#include <asm/syscall.h> /* for syscall_get_* */
3336
3437 /*
3538 * Access another process' address space via ptrace.
....@@ -115,6 +118,9 @@
115118 BUG_ON(!child->ptrace);
116119
117120 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
121
+#ifdef TIF_SYSCALL_EMU
122
+ clear_tsk_thread_flag(child, TIF_SYSCALL_EMU);
123
+#endif
118124
119125 child->parent = child->real_parent;
120126 list_del_init(&child->ptrace_entry);
....@@ -190,14 +196,7 @@
190196 spin_lock_irq(&task->sighand->siglock);
191197 if (task_is_traced(task) && !looks_like_a_spurious_pid(task) &&
192198 !__fatal_signal_pending(task)) {
193
- unsigned long flags;
194
-
195
- raw_spin_lock_irqsave(&task->pi_lock, flags);
196
- if (task->state & __TASK_TRACED)
197
- task->state = __TASK_TRACED;
198
- else
199
- task->saved_state = __TASK_TRACED;
200
- raw_spin_unlock_irqrestore(&task->pi_lock, flags);
199
+ task->state = __TASK_TRACED;
201200 ret = true;
202201 }
203202 spin_unlock_irq(&task->sighand->siglock);
....@@ -207,8 +206,8 @@
207206
208207 static void ptrace_unfreeze_traced(struct task_struct *task)
209208 {
210
- unsigned long flags;
211
- bool frozen = true;
209
+ if (task->state != __TASK_TRACED)
210
+ return;
212211
213212 WARN_ON(!task->ptrace || task->parent != current);
214213
....@@ -217,19 +216,12 @@
217216 * Recheck state under the lock to close this race.
218217 */
219218 spin_lock_irq(&task->sighand->siglock);
220
-
221
- raw_spin_lock_irqsave(&task->pi_lock, flags);
222
- if (task->state == __TASK_TRACED)
223
- task->state = TASK_TRACED;
224
- else if (task->saved_state == __TASK_TRACED)
225
- task->saved_state = TASK_TRACED;
226
- else
227
- frozen = false;
228
- raw_spin_unlock_irqrestore(&task->pi_lock, flags);
229
-
230
- if (frozen && __fatal_signal_pending(task))
231
- wake_up_state(task, __TASK_TRACED);
232
-
219
+ if (task->state == __TASK_TRACED) {
220
+ if (__fatal_signal_pending(task))
221
+ wake_up_state(task, __TASK_TRACED);
222
+ else
223
+ task->state = TASK_TRACED;
224
+ }
233225 spin_unlock_irq(&task->sighand->siglock);
234226 }
235227
....@@ -378,6 +370,26 @@
378370 return !err;
379371 }
380372
373
+static int check_ptrace_options(unsigned long data)
374
+{
375
+ if (data & ~(unsigned long)PTRACE_O_MASK)
376
+ return -EINVAL;
377
+
378
+ if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
379
+ if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
380
+ !IS_ENABLED(CONFIG_SECCOMP))
381
+ return -EINVAL;
382
+
383
+ if (!capable(CAP_SYS_ADMIN))
384
+ return -EPERM;
385
+
386
+ if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
387
+ current->ptrace & PT_SUSPEND_SECCOMP)
388
+ return -EPERM;
389
+ }
390
+ return 0;
391
+}
392
+
381393 static int ptrace_attach(struct task_struct *task, long request,
382394 unsigned long addr,
383395 unsigned long flags)
....@@ -389,8 +401,16 @@
389401 if (seize) {
390402 if (addr != 0)
391403 goto out;
404
+ /*
405
+ * This duplicates the check in check_ptrace_options() because
406
+ * ptrace_attach() and ptrace_setoptions() have historically
407
+ * used different error codes for unknown ptrace options.
408
+ */
392409 if (flags & ~(unsigned long)PTRACE_O_MASK)
393410 goto out;
411
+ retval = check_ptrace_options(flags);
412
+ if (retval)
413
+ return retval;
394414 flags = PT_PTRACED | PT_SEIZED | (flags << PT_OPT_FLAG_SHIFT);
395415 } else {
396416 flags = PT_PTRACED;
....@@ -434,7 +454,7 @@
434454
435455 /* SEIZE doesn't trap tracee on attach */
436456 if (!seize)
437
- send_sig_info(SIGSTOP, SEND_SIG_FORCED, task);
457
+ send_sig_info(SIGSTOP, SEND_SIG_PRIV, task);
438458
439459 spin_lock(&task->sighand->siglock);
440460
....@@ -601,7 +621,7 @@
601621
602622 list_for_each_entry_safe(p, n, &tracer->ptraced, ptrace_entry) {
603623 if (unlikely(p->ptrace & PT_EXITKILL))
604
- send_sig_info(SIGKILL, SEND_SIG_FORCED, p);
624
+ send_sig_info(SIGKILL, SEND_SIG_PRIV, p);
605625
606626 if (__ptrace_detach(tracer, p))
607627 list_add(&p->ptrace_entry, dead);
....@@ -663,22 +683,11 @@
663683 static int ptrace_setoptions(struct task_struct *child, unsigned long data)
664684 {
665685 unsigned flags;
686
+ int ret;
666687
667
- if (data & ~(unsigned long)PTRACE_O_MASK)
668
- return -EINVAL;
669
-
670
- if (unlikely(data & PTRACE_O_SUSPEND_SECCOMP)) {
671
- if (!IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) ||
672
- !IS_ENABLED(CONFIG_SECCOMP))
673
- return -EINVAL;
674
-
675
- if (!capable(CAP_SYS_ADMIN))
676
- return -EPERM;
677
-
678
- if (seccomp_mode(&current->seccomp) != SECCOMP_MODE_DISABLED ||
679
- current->ptrace & PT_SUSPEND_SECCOMP)
680
- return -EPERM;
681
- }
688
+ ret = check_ptrace_options(data);
689
+ if (ret)
690
+ return ret;
682691
683692 /* Avoid intermediate state when all opts are cleared */
684693 flags = child->ptrace;
....@@ -689,7 +698,7 @@
689698 return 0;
690699 }
691700
692
-static int ptrace_getsiginfo(struct task_struct *child, siginfo_t *info)
701
+static int ptrace_getsiginfo(struct task_struct *child, kernel_siginfo_t *info)
693702 {
694703 unsigned long flags;
695704 int error = -ESRCH;
....@@ -705,7 +714,7 @@
705714 return error;
706715 }
707716
708
-static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info)
717
+static int ptrace_setsiginfo(struct task_struct *child, const kernel_siginfo_t *info)
709718 {
710719 unsigned long flags;
711720 int error = -ESRCH;
....@@ -751,7 +760,7 @@
751760 pending = &child->pending;
752761
753762 for (i = 0; i < arg.nr; ) {
754
- siginfo_t info;
763
+ kernel_siginfo_t info;
755764 unsigned long off = arg.off + i;
756765 bool found = false;
757766
....@@ -922,14 +931,107 @@
922931 * to ensure no machine forgets it.
923932 */
924933 EXPORT_SYMBOL_GPL(task_user_regset_view);
925
-#endif
934
+
935
+static unsigned long
936
+ptrace_get_syscall_info_entry(struct task_struct *child, struct pt_regs *regs,
937
+ struct ptrace_syscall_info *info)
938
+{
939
+ unsigned long args[ARRAY_SIZE(info->entry.args)];
940
+ int i;
941
+
942
+ info->op = PTRACE_SYSCALL_INFO_ENTRY;
943
+ info->entry.nr = syscall_get_nr(child, regs);
944
+ syscall_get_arguments(child, regs, args);
945
+ for (i = 0; i < ARRAY_SIZE(args); i++)
946
+ info->entry.args[i] = args[i];
947
+
948
+ /* args is the last field in struct ptrace_syscall_info.entry */
949
+ return offsetofend(struct ptrace_syscall_info, entry.args);
950
+}
951
+
952
+static unsigned long
953
+ptrace_get_syscall_info_seccomp(struct task_struct *child, struct pt_regs *regs,
954
+ struct ptrace_syscall_info *info)
955
+{
956
+ /*
957
+ * As struct ptrace_syscall_info.entry is currently a subset
958
+ * of struct ptrace_syscall_info.seccomp, it makes sense to
959
+ * initialize that subset using ptrace_get_syscall_info_entry().
960
+ * This can be reconsidered in the future if these structures
961
+ * diverge significantly enough.
962
+ */
963
+ ptrace_get_syscall_info_entry(child, regs, info);
964
+ info->op = PTRACE_SYSCALL_INFO_SECCOMP;
965
+ info->seccomp.ret_data = child->ptrace_message;
966
+
967
+ /* ret_data is the last field in struct ptrace_syscall_info.seccomp */
968
+ return offsetofend(struct ptrace_syscall_info, seccomp.ret_data);
969
+}
970
+
971
+static unsigned long
972
+ptrace_get_syscall_info_exit(struct task_struct *child, struct pt_regs *regs,
973
+ struct ptrace_syscall_info *info)
974
+{
975
+ info->op = PTRACE_SYSCALL_INFO_EXIT;
976
+ info->exit.rval = syscall_get_error(child, regs);
977
+ info->exit.is_error = !!info->exit.rval;
978
+ if (!info->exit.is_error)
979
+ info->exit.rval = syscall_get_return_value(child, regs);
980
+
981
+ /* is_error is the last field in struct ptrace_syscall_info.exit */
982
+ return offsetofend(struct ptrace_syscall_info, exit.is_error);
983
+}
984
+
985
+static int
986
+ptrace_get_syscall_info(struct task_struct *child, unsigned long user_size,
987
+ void __user *datavp)
988
+{
989
+ struct pt_regs *regs = task_pt_regs(child);
990
+ struct ptrace_syscall_info info = {
991
+ .op = PTRACE_SYSCALL_INFO_NONE,
992
+ .arch = syscall_get_arch(child),
993
+ .instruction_pointer = instruction_pointer(regs),
994
+ .stack_pointer = user_stack_pointer(regs),
995
+ };
996
+ unsigned long actual_size = offsetof(struct ptrace_syscall_info, entry);
997
+ unsigned long write_size;
998
+
999
+ /*
1000
+ * This does not need lock_task_sighand() to access
1001
+ * child->last_siginfo because ptrace_freeze_traced()
1002
+ * called earlier by ptrace_check_attach() ensures that
1003
+ * the tracee cannot go away and clear its last_siginfo.
1004
+ */
1005
+ switch (child->last_siginfo ? child->last_siginfo->si_code : 0) {
1006
+ case SIGTRAP | 0x80:
1007
+ switch (child->ptrace_message) {
1008
+ case PTRACE_EVENTMSG_SYSCALL_ENTRY:
1009
+ actual_size = ptrace_get_syscall_info_entry(child, regs,
1010
+ &info);
1011
+ break;
1012
+ case PTRACE_EVENTMSG_SYSCALL_EXIT:
1013
+ actual_size = ptrace_get_syscall_info_exit(child, regs,
1014
+ &info);
1015
+ break;
1016
+ }
1017
+ break;
1018
+ case SIGTRAP | (PTRACE_EVENT_SECCOMP << 8):
1019
+ actual_size = ptrace_get_syscall_info_seccomp(child, regs,
1020
+ &info);
1021
+ break;
1022
+ }
1023
+
1024
+ write_size = min(actual_size, user_size);
1025
+ return copy_to_user(datavp, &info, write_size) ? -EFAULT : actual_size;
1026
+}
1027
+#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
9261028
9271029 int ptrace_request(struct task_struct *child, long request,
9281030 unsigned long addr, unsigned long data)
9291031 {
9301032 bool seized = child->ptrace & PT_SEIZED;
9311033 int ret = -EIO;
932
- siginfo_t siginfo, *si;
1034
+ kernel_siginfo_t siginfo, *si;
9331035 void __user *datavp = (void __user *) data;
9341036 unsigned long __user *datalp = datavp;
9351037 unsigned long flags;
....@@ -963,9 +1065,8 @@
9631065 break;
9641066
9651067 case PTRACE_SETSIGINFO:
966
- if (copy_from_user(&siginfo, datavp, sizeof siginfo))
967
- ret = -EFAULT;
968
- else
1068
+ ret = copy_siginfo_from_user(&siginfo, datavp);
1069
+ if (!ret)
9691070 ret = ptrace_setsiginfo(child, &siginfo);
9701071 break;
9711072
....@@ -1118,9 +1219,8 @@
11181219 return ptrace_resume(child, request, data);
11191220
11201221 case PTRACE_KILL:
1121
- if (child->exit_state) /* already dead */
1122
- return 0;
1123
- return ptrace_resume(child, request, SIGKILL);
1222
+ send_sig_info(SIGKILL, SEND_SIG_NOINFO, child);
1223
+ return 0;
11241224
11251225 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
11261226 case PTRACE_GETREGSET:
....@@ -1128,7 +1228,7 @@
11281228 struct iovec kiov;
11291229 struct iovec __user *uiov = datavp;
11301230
1131
- if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1231
+ if (!access_ok(uiov, sizeof(*uiov)))
11321232 return -EFAULT;
11331233
11341234 if (__get_user(kiov.iov_base, &uiov->iov_base) ||
....@@ -1140,6 +1240,10 @@
11401240 ret = __put_user(kiov.iov_len, &uiov->iov_len);
11411241 break;
11421242 }
1243
+
1244
+ case PTRACE_GET_SYSCALL_INFO:
1245
+ ret = ptrace_get_syscall_info(child, addr, datavp);
1246
+ break;
11431247 #endif
11441248
11451249 case PTRACE_SECCOMP_GET_FILTER:
....@@ -1235,7 +1339,7 @@
12351339 {
12361340 compat_ulong_t __user *datap = compat_ptr(data);
12371341 compat_ulong_t word;
1238
- siginfo_t siginfo;
1342
+ kernel_siginfo_t siginfo;
12391343 int ret;
12401344
12411345 switch (request) {
....@@ -1269,10 +1373,9 @@
12691373 break;
12701374
12711375 case PTRACE_SETSIGINFO:
1272
- if (copy_siginfo_from_user32(
1273
- &siginfo, (struct compat_siginfo __user *) datap))
1274
- ret = -EFAULT;
1275
- else
1376
+ ret = copy_siginfo_from_user32(
1377
+ &siginfo, (struct compat_siginfo __user *) datap);
1378
+ if (!ret)
12761379 ret = ptrace_setsiginfo(child, &siginfo);
12771380 break;
12781381 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
....@@ -1285,7 +1388,7 @@
12851388 compat_uptr_t ptr;
12861389 compat_size_t len;
12871390
1288
- if (!access_ok(VERIFY_WRITE, uiov, sizeof(*uiov)))
1391
+ if (!access_ok(uiov, sizeof(*uiov)))
12891392 return -EFAULT;
12901393
12911394 if (__get_user(ptr, &uiov->iov_base) ||