Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #define _GNU_SOURCE
0004 #include <errno.h>
0005 #include <fcntl.h>
0006 #include <limits.h>
0007 #include <linux/types.h>
0008 #include <sched.h>
0009 #include <signal.h>
0010 #include <stdio.h>
0011 #include <stdlib.h>
0012 #include <string.h>
0013 #include <syscall.h>
0014 #include <sys/prctl.h>
0015 #include <sys/wait.h>
0016 #include <unistd.h>
0017 #include <sys/socket.h>
0018 #include <linux/kcmp.h>
0019 
0020 #include "pidfd.h"
0021 #include "../kselftest_harness.h"
0022 
0023 /*
0024  * UNKNOWN_FD is an fd number that should never exist in the child, as it is
0025  * used to check the negative case.
0026  */
0027 #define UNKNOWN_FD 111
0028 #define UID_NOBODY 65535
0029 
0030 static int sys_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
0031             unsigned long idx2)
0032 {
0033     return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
0034 }
0035 
0036 static int __child(int sk, int memfd)
0037 {
0038     int ret;
0039     char buf;
0040 
0041     /*
0042      * Ensure we don't leave around a bunch of orphaned children if our
0043      * tests fail.
0044      */
0045     ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
0046     if (ret) {
0047         fprintf(stderr, "%s: Child could not set DEATHSIG\n",
0048             strerror(errno));
0049         return -1;
0050     }
0051 
0052     ret = send(sk, &memfd, sizeof(memfd), 0);
0053     if (ret != sizeof(memfd)) {
0054         fprintf(stderr, "%s: Child failed to send fd number\n",
0055             strerror(errno));
0056         return -1;
0057     }
0058 
0059     /*
0060      * The fixture setup is completed at this point. The tests will run.
0061      *
0062      * This blocking recv enables the parent to message the child.
0063      * Either we will read 'P' off of the sk, indicating that we need
0064      * to disable ptrace, or we will read a 0, indicating that the other
0065      * side has closed the sk. This occurs during fixture teardown time,
0066      * indicating that the child should exit.
0067      */
0068     while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) {
0069         if (buf == 'P') {
0070             ret = prctl(PR_SET_DUMPABLE, 0);
0071             if (ret < 0) {
0072                 fprintf(stderr,
0073                     "%s: Child failed to disable ptrace\n",
0074                     strerror(errno));
0075                 return -1;
0076             }
0077         } else {
0078             fprintf(stderr, "Child received unknown command %c\n",
0079                 buf);
0080             return -1;
0081         }
0082         ret = send(sk, &buf, sizeof(buf), 0);
0083         if (ret != 1) {
0084             fprintf(stderr, "%s: Child failed to ack\n",
0085                 strerror(errno));
0086             return -1;
0087         }
0088     }
0089     if (ret < 0) {
0090         fprintf(stderr, "%s: Child failed to read from socket\n",
0091             strerror(errno));
0092         return -1;
0093     }
0094 
0095     return 0;
0096 }
0097 
0098 static int child(int sk)
0099 {
0100     int memfd, ret;
0101 
0102     memfd = sys_memfd_create("test", 0);
0103     if (memfd < 0) {
0104         fprintf(stderr, "%s: Child could not create memfd\n",
0105             strerror(errno));
0106         ret = -1;
0107     } else {
0108         ret = __child(sk, memfd);
0109         close(memfd);
0110     }
0111 
0112     close(sk);
0113     return ret;
0114 }
0115 
0116 FIXTURE(child)
0117 {
0118     /*
0119      * remote_fd is the number of the FD which we are trying to retrieve
0120      * from the child.
0121      */
0122     int remote_fd;
0123     /* pid points to the child which we are fetching FDs from */
0124     pid_t pid;
0125     /* pidfd is the pidfd of the child */
0126     int pidfd;
0127     /*
0128      * sk is our side of the socketpair used to communicate with the child.
0129      * When it is closed, the child will exit.
0130      */
0131     int sk;
0132 };
0133 
0134 FIXTURE_SETUP(child)
0135 {
0136     int ret, sk_pair[2];
0137 
0138     ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) {
0139         TH_LOG("%s: failed to create socketpair", strerror(errno));
0140     }
0141     self->sk = sk_pair[0];
0142 
0143     self->pid = fork();
0144     ASSERT_GE(self->pid, 0);
0145 
0146     if (self->pid == 0) {
0147         close(sk_pair[0]);
0148         if (child(sk_pair[1]))
0149             _exit(EXIT_FAILURE);
0150         _exit(EXIT_SUCCESS);
0151     }
0152 
0153     close(sk_pair[1]);
0154 
0155     self->pidfd = sys_pidfd_open(self->pid, 0);
0156     ASSERT_GE(self->pidfd, 0);
0157 
0158     /*
0159      * Wait for the child to complete setup. It'll send the remote memfd's
0160      * number when ready.
0161      */
0162     ret = recv(sk_pair[0], &self->remote_fd, sizeof(self->remote_fd), 0);
0163     ASSERT_EQ(sizeof(self->remote_fd), ret);
0164 }
0165 
0166 FIXTURE_TEARDOWN(child)
0167 {
0168     EXPECT_EQ(0, close(self->pidfd));
0169     EXPECT_EQ(0, close(self->sk));
0170 
0171     EXPECT_EQ(0, wait_for_pid(self->pid));
0172 }
0173 
0174 TEST_F(child, disable_ptrace)
0175 {
0176     int uid, fd;
0177     char c;
0178 
0179     /*
0180      * Turn into nobody if we're root, to avoid CAP_SYS_PTRACE
0181      *
0182      * The tests should run in their own process, so even this test fails,
0183      * it shouldn't result in subsequent tests failing.
0184      */
0185     uid = getuid();
0186     if (uid == 0)
0187         ASSERT_EQ(0, seteuid(UID_NOBODY));
0188 
0189     ASSERT_EQ(1, send(self->sk, "P", 1, 0));
0190     ASSERT_EQ(1, recv(self->sk, &c, 1, 0));
0191 
0192     fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
0193     EXPECT_EQ(-1, fd);
0194     EXPECT_EQ(EPERM, errno);
0195 
0196     if (uid == 0)
0197         ASSERT_EQ(0, seteuid(0));
0198 }
0199 
0200 TEST_F(child, fetch_fd)
0201 {
0202     int fd, ret;
0203 
0204     fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
0205     ASSERT_GE(fd, 0);
0206 
0207     ret = sys_kcmp(getpid(), self->pid, KCMP_FILE, fd, self->remote_fd);
0208     if (ret < 0 && errno == ENOSYS)
0209         SKIP(return, "kcmp() syscall not supported");
0210     EXPECT_EQ(ret, 0);
0211 
0212     ret = fcntl(fd, F_GETFD);
0213     ASSERT_GE(ret, 0);
0214     EXPECT_GE(ret & FD_CLOEXEC, 0);
0215 
0216     close(fd);
0217 }
0218 
0219 TEST_F(child, test_unknown_fd)
0220 {
0221     int fd;
0222 
0223     fd = sys_pidfd_getfd(self->pidfd, UNKNOWN_FD, 0);
0224     EXPECT_EQ(-1, fd) {
0225         TH_LOG("getfd succeeded while fetching unknown fd");
0226     };
0227     EXPECT_EQ(EBADF, errno) {
0228         TH_LOG("%s: getfd did not get EBADF", strerror(errno));
0229     }
0230 }
0231 
0232 TEST(flags_set)
0233 {
0234     ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, 1));
0235     EXPECT_EQ(errno, EINVAL);
0236 }
0237 
0238 #if __NR_pidfd_getfd == -1
0239 int main(void)
0240 {
0241     fprintf(stderr, "__NR_pidfd_getfd undefined. The pidfd_getfd syscall is unavailable. Test aborting\n");
0242     return KSFT_SKIP;
0243 }
0244 #else
0245 TEST_HARNESS_MAIN
0246 #endif