Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp20779041ybl; Sun, 5 Jan 2020 11:09:16 -0800 (PST) X-Google-Smtp-Source: APXvYqzrSAuCokfCTSaiziNGj7LLU2Z7YZuv9Zk06OhBxxDHXTBti34h41xQfhjX4hOCLXGHUTzs X-Received: by 2002:a9d:7305:: with SMTP id e5mr106427755otk.64.1578251356238; Sun, 05 Jan 2020 11:09:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1578251356; cv=none; d=google.com; s=arc-20160816; b=KcHpmYXFtR2Fe09ndpLYhRuAP/IhXfVz/W9D9bCd673tcusFrincqwjIFK7Md9z07J b/klnJuERDpIQYZtbhuhqDrVEj3WybjH132DlSKIL2xdhtkPLWlai4zSeRvP+ZyKGBqy W9madL7Zot2imFQasl7YgSTyqllJvAUGOi6hbYGQqkzBSeFgtw5VnO1VMYPD/xCovlPt 2rJbLUsypNXLEQDvXAdrk6EmXevpT6kPSgPdEI/V9ruJRw/pCe9Zw4m7cc7mV1SxL0Ge ctY3rDpRhgPs6T64kCYRx5uHjMv75hhtTOTmPoPglao+1SaLz/mtL6SA03GHAg8yJSKH OWrw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=BCTcRrTcnLIdSFlZUFovhn7ud4Us6Qx7aWDrGSYm67Q=; b=Sg/Xu5MQUvrFtJgxF4LDz20QxgHQGAytQ20AZoIIHOlth8zDS3lzEAS1l7ruRlM9NV L4Ojut77hDVdHva0TK0hTx389CKw9+Unr2uO6DfpHeU+0v06KStz6QsWgLwSQFGu3BE+ kfVnCwFiLpN6GMowCechTAi7aIH+nfq3vhX3Rk2vKHJpBfS79fL9794PCDi0COkJufGc StcOl0xSL4JzHzZUdZnNVgNZPJjeVOY4DxBT/CSt6Y5DxDjxTGKAdkXo0GjNpgqEmgzR UIK5pPky1A0SKqExys7/AwneS18uOTsDaEjlp7SJYC84zfZAO85wJ7WMB7RJ1rj2drSy vVFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=qYk0nmgV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k15si26995231oij.134.2020.01.05.11.09.02; Sun, 05 Jan 2020 11:09:16 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@sargun.me header.s=google header.b=qYk0nmgV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726515AbgAETIR (ORCPT + 99 others); Sun, 5 Jan 2020 14:08:17 -0500 Received: from mail-io1-f67.google.com ([209.85.166.67]:43902 "EHLO mail-io1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726481AbgAETIQ (ORCPT ); Sun, 5 Jan 2020 14:08:16 -0500 Received: by mail-io1-f67.google.com with SMTP id n21so44968496ioo.10 for ; Sun, 05 Jan 2020 11:08:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sargun.me; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=BCTcRrTcnLIdSFlZUFovhn7ud4Us6Qx7aWDrGSYm67Q=; b=qYk0nmgVpoY9YoDHOxjF4mOC4ayRwWpoRM33m7Aq4+32Ri09ouv1zIeFJrMg0N803o EflL3e+64TtZ1YBbJaz5m45rlVlVsHD7KOhs9SnDIFvBJ5uaWZeNUtowdshTceV7mldH Yjz4JlEHPtoW2z8U+Wj/G1+JMj9K0Jp5JKiIE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=BCTcRrTcnLIdSFlZUFovhn7ud4Us6Qx7aWDrGSYm67Q=; b=WkabmPJ6qTeM7Z4y9ytvcOLqhV6smWAgs+j8dGmrDD4qW1Equ1d62yr3Bf1ViNDgGt W2ZUuTg1BR94cZZkKDq9r9EtdT+18UNSHB0mLFv80GWJR6aaT/mRZ2R6ZyE+edx//yFP N622gjIBWoiUgBFL57ex5WDOXMGPoaAXmZ7OT8L43yLA8E+sVMDV/N0sSJ4fWO1HESD0 zozjR/GIvhLuNz9k1DXLwJvr0Iu8rGTFxeHH2J/t/4uQikqrX3f3GR2h3hlUpYi2F35Q hW6U2c+ULJLCwEEVjLM+nY6TV/lKbbYKjIPEuOgtM3ebNi86t1f+PPSgkq0yw5s3UGtB 4bAA== X-Gm-Message-State: APjAAAXMBbCVgBrbU26fD+dwi2YgNixuAuxJQGXB39tfg+Y/lh35reGZ GUzV6DTdwCJrdUd2jzfS7w+rCnNdIyfg/g== X-Received: by 2002:a02:7310:: with SMTP id y16mr8843445jab.133.1578251295532; Sun, 05 Jan 2020 11:08:15 -0800 (PST) Received: from ircssh-2.c.rugged-nimbus-611.internal (80.60.198.104.bc.googleusercontent.com. [104.198.60.80]) by smtp.gmail.com with ESMTPSA id e1sm23153880ill.47.2020.01.05.11.08.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 05 Jan 2020 11:08:14 -0800 (PST) Date: Sun, 5 Jan 2020 19:08:13 +0000 From: Sargun Dhillon To: linux-kernel@vger.kernel.org, containers@lists.linux-foundation.org, linux-api@vger.kernel.org, linux-fsdevel@vger.kernel.org Cc: Sargun Dhillon , tycho@tycho.ws, jannh@google.com, cyphar@cyphar.com, christian.brauner@ubuntu.com, oleg@redhat.com, luto@amacapital.net, viro@zeniv.linux.org.uk, gpascutto@mozilla.com, ealvarez@mozilla.com, fweimer@redhat.com, jld@mozilla.com, arnd@arndb.de Subject: Re: [PATCH v8 3/3] test: Add test for pidfd getfd Message-ID: <20200105190812.GC8522@ircssh-2.c.rugged-nimbus-611.internal> References: <20200103162928.5271-1-sargun@sargun.me> <20200103162928.5271-4-sargun@sargun.me> <20200105142019.umls5ff4b5433u6k@wittgenstein> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200105142019.umls5ff4b5433u6k@wittgenstein> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Jan 05, 2020 at 03:20:23PM +0100, Christian Brauner wrote: > On Fri, Jan 03, 2020 at 08:29:28AM -0800, Sargun Dhillon wrote: > > +static int sys_pidfd_getfd(int pidfd, int fd, int flags) > > +{ > > + return syscall(__NR_pidfd_getfd, pidfd, fd, flags); > > +} > > I think you can move this to the pidfd.h header as: > > static inline int sys_pidfd_getfd(int pidfd, int fd, int flags) > { > return syscall(__NR_pidfd_getfd, pidfd, fd, flags); > } > > Note, this also needs an > > #ifndef __NR_pidfd_getfd > __NR_pidfd_getfd -1 > #endif > so that compilation doesn't fail. > I'll go ahead and move this into pidfd.h, and follow the pattern there. I don't think it's worth checking if each time the return code is ENOSYS. Does it make sense to add something like: #ifdef __NR_pidfd_getfd TEST_HARNESS_MAIN #else int main(void) { fprintf(stderr, "pidfd_getfd syscall not supported\n"); return KSFT_SKIP; } #endif to short-circuit the entire test suite? > > + > > +static int sys_memfd_create(const char *name, unsigned int flags) > > +{ > > + return syscall(__NR_memfd_create, name, flags); > > +} > > + > > +static int __child(int sk, int memfd) > > +{ > > + int ret; > > + char buf; > > + > > + /* > > + * Ensure we don't leave around a bunch of orphaned children if our > > + * tests fail. > > + */ > > + ret = prctl(PR_SET_PDEATHSIG, SIGKILL); > > + if (ret) { > > + fprintf(stderr, "%s: Child could not set DEATHSIG\n", > > + strerror(errno)); > > + return EXIT_FAILURE; > > return -1 > > > + } > > + > > + ret = send(sk, &memfd, sizeof(memfd), 0); > > + if (ret != sizeof(memfd)) { > > + fprintf(stderr, "%s: Child failed to send fd number\n", > > + strerror(errno)); > > + return EXIT_FAILURE; > > return -1 > > > + } > > + > > + while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) { > > + if (buf == 'P') { > > + ret = prctl(PR_SET_DUMPABLE, 0); > > + if (ret < 0) { > > + fprintf(stderr, > > + "%s: Child failed to disable ptrace\n", > > + strerror(errno)); > > + return EXIT_FAILURE; > > return -1 > > > + } > > + } else { > > + fprintf(stderr, "Child received unknown command %c\n", > > + buf); > > + return EXIT_FAILURE; > > return -1 > > > + } > > + ret = send(sk, &buf, sizeof(buf), 0); > > + if (ret != 1) { > > + fprintf(stderr, "%s: Child failed to ack\n", > > + strerror(errno)); > > + return EXIT_FAILURE; > > return -1 > > > + } > > + } > > + > > + if (ret < 0) { > > + fprintf(stderr, "%s: Child failed to read from socket\n", > > + strerror(errno)); > > Is this intentional that this is no failure? > My thought here, is the only case where this should happen is if the "ptrace command" was not properly "transmitted", and the ptrace test itself would fail. I can add an explicit exit failure here. > > + } > > + > > + return EXIT_SUCCESS; > > return 0 > > > +} > > + > > +static int child(int sk) > > +{ > > + int memfd, ret; > > + > > + memfd = sys_memfd_create("test", 0); > > + if (memfd < 0) { > > + fprintf(stderr, "%s: Child could not create memfd\n", > > + strerror(errno)); > > + ret = EXIT_FAILURE; > > ret = -1; > > > + } else { > > + ret = __child(sk, memfd); > > + close(memfd); > > + } > > + > > + close(sk); > > + return ret; > > +} > > + > > +FIXTURE(child) > > +{ > > + pid_t pid; > > + int pidfd, sk, remote_fd; > > +}; > > + > > +FIXTURE_SETUP(child) > > +{ > > + int ret, sk_pair[2]; > > + > > + ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) > > + { > > + TH_LOG("%s: failed to create socketpair", strerror(errno)); > > + } > > + self->sk = sk_pair[0]; > > + > > + self->pid = fork(); > > + ASSERT_GE(self->pid, 0); > > + > > + if (self->pid == 0) { > > + close(sk_pair[0]); > > + exit(child(sk_pair[1])); > > if (child(sk_pair[1])) > _exit(EXIT_FAILURE); > _exit(EXIT_SUCCESS); > > I would like to only use exit macros where one actually calls > {_}exit()s. It makes the logic easier to follow and ensures that one > doesn't accidently do an exit(-21345) or something (e.g. when adding new > code). > > > + } > > + > > + close(sk_pair[1]); > > + > > + self->pidfd = sys_pidfd_open(self->pid, 0); > > + ASSERT_GE(self->pidfd, 0); > > + > > + /* > > + * Wait for the child to complete setup. It'll send the remote memfd's > > + * number when ready. > > + */ > > + ret = recv(sk_pair[0], &self->remote_fd, sizeof(self->remote_fd), 0); > > + ASSERT_EQ(sizeof(self->remote_fd), ret); > > +} > > + > > +FIXTURE_TEARDOWN(child) > > +{ > > + int status; > > + > > + EXPECT_EQ(0, close(self->pidfd)); > > + EXPECT_EQ(0, close(self->sk)); > > + > > + EXPECT_EQ(waitpid(self->pid, &status, 0), self->pid); > > + EXPECT_EQ(true, WIFEXITED(status)); > > + EXPECT_EQ(0, WEXITSTATUS(status)); > > +} > > + > > +TEST_F(child, disable_ptrace) > > +{ > > + int uid, fd; > > + char c; > > + > > + /* > > + * Turn into nobody if we're root, to avoid CAP_SYS_PTRACE > > + * > > + * The tests should run in their own process, so even this test fails, > > + * it shouldn't result in subsequent tests failing. > > + */ > > + uid = getuid(); > > + if (uid == 0) > > + ASSERT_EQ(0, seteuid(USHRT_MAX)); > > Hm, isn't it safer to do 65535 explicitly? Since USHRT_MAX can > technically be greater than 65535. > I borrowed this from the BPF tests. I can hardcode something like: #define NOBODY_UID 65535 and setuid to that, if you think it's safer? > > + > > + ASSERT_EQ(1, send(self->sk, "P", 1, 0)); > > + ASSERT_EQ(1, recv(self->sk, &c, 1, 0)); > > + > > + fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0); > > + EXPECT_EQ(-1, fd); > > + EXPECT_EQ(EPERM, errno); > > + > > + if (uid == 0) > > + ASSERT_EQ(0, seteuid(0)); > > +} > > + > > +TEST_F(child, fetch_fd) > > +{ > > + int fd, ret; > > + > > + fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0); > > + ASSERT_GE(fd, 0); > > + > > + EXPECT_EQ(0, sys_kcmp(getpid(), self->pid, KCMP_FILE, fd, self->remote_fd)); > > So most of these tests seem to take place when the child has already > called exit() - or at least it's very likely that the child has already > called exit() - and remains a zombie. That's not ideal because > that's not the common scenario/use-case. Usually the task of which we > want to get an fd will be alive. Also, if the child has already called > exit(), by the time it returns to userspace it should have already > called exit_files() and so I wonder whether this test would fail if it's > run after the child has exited. Maybe I'm missing something here... Is > there some ordering enforced by TEST_F()? Yeah, I think perhaps I was being too clever. The timeline roughly goes something like this: # Fixture bringup [parent] creates socket_pair [parent] forks, and passes pair down to child [parent] waits to read sizeof(int) from the sk_pair [child] creates memfd [__child] sends local memfd number to parent via sk_pair [__child] waits to read from sk_pair [parent] reads remote memfd number from socket # Test [parent] performs tests # Fixture teardown [parent] closes sk_pair [__child] reads 0 from recv on sk_pair, implies the other end is closed [__child] Returns / exits 0 [parent] Reaps child / reads exit code --- The one case where this is not true, is if the parent sends 'P' to the sk pair, it triggers setting PR_SET_DUMPABLE to 0, and then resumes waiting for the fd to close. Maybe I'm being too clever? Instead, the alternative was to send explicit stop / start messages across the sk_pair, but that got kind of ugly. Do you have a better suggestion? > > Also, what does self->pid point to? The fd of the already exited child? It's just the pid of the child. pidfd is the fd of the (unexited) child.