Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #define _GNU_SOURCE
0003 #include <stdio.h>
0004 #include <signal.h>
0005 #include <unistd.h>
0006 #include <errno.h>
0007 #include <linux/types.h>
0008 #include <sys/wait.h>
0009 #include <sys/syscall.h>
0010 #include <sys/user.h>
0011 #include <sys/mman.h>
0012 
0013 #include "linux/ptrace.h"
0014 
0015 static int sys_rt_sigqueueinfo(pid_t tgid, int sig, siginfo_t *uinfo)
0016 {
0017     return syscall(SYS_rt_sigqueueinfo, tgid, sig, uinfo);
0018 }
0019 
0020 static int sys_rt_tgsigqueueinfo(pid_t tgid, pid_t tid,
0021                     int sig, siginfo_t *uinfo)
0022 {
0023     return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, uinfo);
0024 }
0025 
0026 static int sys_ptrace(int request, pid_t pid, void *addr, void *data)
0027 {
0028     return syscall(SYS_ptrace, request, pid, addr, data);
0029 }
0030 
0031 #define SIGNR 10
0032 #define TEST_SICODE_PRIV    -1
0033 #define TEST_SICODE_SHARE   -2
0034 
0035 #ifndef PAGE_SIZE
0036 #define PAGE_SIZE sysconf(_SC_PAGESIZE)
0037 #endif
0038 
0039 #define err(fmt, ...)                       \
0040         fprintf(stderr,                 \
0041             "Error (%s:%d): " fmt,          \
0042             __FILE__, __LINE__, ##__VA_ARGS__)
0043 
0044 static int check_error_paths(pid_t child)
0045 {
0046     struct ptrace_peeksiginfo_args arg;
0047     int ret, exit_code = -1;
0048     void *addr_rw, *addr_ro;
0049 
0050     /*
0051      * Allocate two contiguous pages. The first one is for read-write,
0052      * another is for read-only.
0053      */
0054     addr_rw = mmap(NULL, 2 * PAGE_SIZE, PROT_READ | PROT_WRITE,
0055                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
0056     if (addr_rw == MAP_FAILED) {
0057         err("mmap() failed: %m\n");
0058         return 1;
0059     }
0060 
0061     addr_ro = mmap(addr_rw + PAGE_SIZE, PAGE_SIZE, PROT_READ,
0062             MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
0063     if (addr_ro == MAP_FAILED) {
0064         err("mmap() failed: %m\n");
0065         goto out;
0066     }
0067 
0068     arg.nr = SIGNR;
0069     arg.off = 0;
0070 
0071     /* Unsupported flags */
0072     arg.flags = ~0;
0073     ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_rw);
0074     if (ret != -1 || errno != EINVAL) {
0075         err("sys_ptrace() returns %d (expected -1),"
0076                 " errno %d (expected %d): %m\n",
0077                 ret, errno, EINVAL);
0078         goto out;
0079     }
0080     arg.flags = 0;
0081 
0082     /* A part of the buffer is read-only */
0083     ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg,
0084                     addr_ro - sizeof(siginfo_t) * 2);
0085     if (ret != 2) {
0086         err("sys_ptrace() returns %d (expected 2): %m\n", ret);
0087         goto out;
0088     }
0089 
0090     /* Read-only buffer */
0091     ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, addr_ro);
0092     if (ret != -1 && errno != EFAULT) {
0093         err("sys_ptrace() returns %d (expected -1),"
0094                 " errno %d (expected %d): %m\n",
0095                 ret, errno, EFAULT);
0096         goto out;
0097     }
0098 
0099     exit_code = 0;
0100 out:
0101     munmap(addr_rw, 2 * PAGE_SIZE);
0102     return exit_code;
0103 }
0104 
0105 int check_direct_path(pid_t child, int shared, int nr)
0106 {
0107     struct ptrace_peeksiginfo_args arg = {.flags = 0, .nr = nr, .off = 0};
0108     int i, j, ret, exit_code = -1;
0109     siginfo_t siginfo[SIGNR];
0110     int si_code;
0111 
0112     if (shared == 1) {
0113         arg.flags = PTRACE_PEEKSIGINFO_SHARED;
0114         si_code = TEST_SICODE_SHARE;
0115     } else {
0116         arg.flags = 0;
0117         si_code = TEST_SICODE_PRIV;
0118     }
0119 
0120     for (i = 0; i < SIGNR; ) {
0121         arg.off = i;
0122         ret = sys_ptrace(PTRACE_PEEKSIGINFO, child, &arg, siginfo);
0123         if (ret == -1) {
0124             err("ptrace() failed: %m\n");
0125             goto out;
0126         }
0127 
0128         if (ret == 0)
0129             break;
0130 
0131         for (j = 0; j < ret; j++, i++) {
0132             if (siginfo[j].si_code == si_code &&
0133                 siginfo[j].si_int == i)
0134                 continue;
0135 
0136             err("%d: Wrong siginfo i=%d si_code=%d si_int=%d\n",
0137                  shared, i, siginfo[j].si_code, siginfo[j].si_int);
0138             goto out;
0139         }
0140     }
0141 
0142     if (i != SIGNR) {
0143         err("Only %d signals were read\n", i);
0144         goto out;
0145     }
0146 
0147     exit_code = 0;
0148 out:
0149     return exit_code;
0150 }
0151 
0152 int main(int argc, char *argv[])
0153 {
0154     siginfo_t siginfo[SIGNR];
0155     int i, exit_code = 1;
0156     sigset_t blockmask;
0157     pid_t child;
0158 
0159     sigemptyset(&blockmask);
0160     sigaddset(&blockmask, SIGRTMIN);
0161     sigprocmask(SIG_BLOCK, &blockmask, NULL);
0162 
0163     child = fork();
0164     if (child == -1) {
0165         err("fork() failed: %m");
0166         return 1;
0167     } else if (child == 0) {
0168         pid_t ppid = getppid();
0169         while (1) {
0170             if (ppid != getppid())
0171                 break;
0172             sleep(1);
0173         }
0174         return 1;
0175     }
0176 
0177     /* Send signals in process-wide and per-thread queues */
0178     for (i = 0; i < SIGNR; i++) {
0179         siginfo->si_code = TEST_SICODE_SHARE;
0180         siginfo->si_int = i;
0181         sys_rt_sigqueueinfo(child, SIGRTMIN, siginfo);
0182 
0183         siginfo->si_code = TEST_SICODE_PRIV;
0184         siginfo->si_int = i;
0185         sys_rt_tgsigqueueinfo(child, child, SIGRTMIN, siginfo);
0186     }
0187 
0188     if (sys_ptrace(PTRACE_ATTACH, child, NULL, NULL) == -1)
0189         return 1;
0190 
0191     waitpid(child, NULL, 0);
0192 
0193     /* Dump signals one by one*/
0194     if (check_direct_path(child, 0, 1))
0195         goto out;
0196     /* Dump all signals for one call */
0197     if (check_direct_path(child, 0, SIGNR))
0198         goto out;
0199 
0200     /*
0201      * Dump signal from the process-wide queue.
0202      * The number of signals is not multible to the buffer size
0203      */
0204     if (check_direct_path(child, 1, 3))
0205         goto out;
0206 
0207     if (check_error_paths(child))
0208         goto out;
0209 
0210     printf("PASS\n");
0211     exit_code = 0;
0212 out:
0213     if (sys_ptrace(PTRACE_KILL, child, NULL, NULL) == -1)
0214         return 1;
0215 
0216     waitpid(child, NULL, 0);
0217 
0218     return exit_code;
0219 }