Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp1085826rwp; Thu, 13 Jul 2023 06:04:25 -0700 (PDT) X-Google-Smtp-Source: APBJJlFnesdTHEYVkos7/kC/4/wt7mItla5gCeck7kPB7MmFGQit1cV5RBQUwg2w4d4rNBJjjxs8 X-Received: by 2002:a05:6a20:3cab:b0:130:835b:e260 with SMTP id b43-20020a056a203cab00b00130835be260mr1009803pzj.52.1689253465392; Thu, 13 Jul 2023 06:04:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689253465; cv=none; d=google.com; s=arc-20160816; b=T4mX+VSJwRVDiQBIpotPgnk1B6JTBIuGCKwZfpMcYO6XOT5+1cQNNQli6goKiBj5z5 SrYkG5evw6Lmm9yAQhlyZcBv1xeSbI1GYOZ7Mu+orF0tI70njT+LvayW1psNVnAU9uzS NEwisBBmAqmHvwA1Mq8yBmwLiDU0+jolSo9Ba5q6TcNB2LAYIB9D/B+n3uMsE8qWSYmg DyaFBkOoB20xZ4gOQGF0MAbyELOyNEL679uWihQxrYOIZbVS4boCoLWdS8YR1yRUjk17 3TIr3oIICkib6VhbgoychTTPKVlQLq2nPueBhQ4BMoFOwrA61fAneIpnP/az2lpvCuuK sNXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7MDWay4mdZ4dX5UFkyfHpo9oL8sgzdNlzmnFUEm5h/0=; fh=VZXbunz9VH4tTxmlsUxB2cA4XeNqu45kSHz3Xw3uO1I=; b=CS9uG40iEQDD6vS4DaFvxu7zJx8kPWs5XIJB5MnXMercKu+jYYjolaJweDfw/UGvQL t8R2ZoZpJdkfaAcQi6ijQgm1BQtzXSarI1HCMTQM6pDYj1eri+OReYgYuutzutrYSz0r yax/vHuhIfNgrtgwwzPtfC+GTX4a18jaBSZYvln15lOCJj+7VJzz5QIADEKePGy8R+SW BxNhMhTfdy8IVlkVC8LkMARP2WKqvdfLFx6PLCZfrI/cTf5ZRIsmZHVt3KyovKSTi6ao O/aOX0yV6WFI7LwdT/YNrm6NBS7FSzJjURjMo8j4HhRjWsGrxXJ7EAfUQH4VQHVtWGb2 ATFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@t-8ch.de header.s=mail header.b="QSW/8Nr5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 5-20020a630d45000000b0053059dfafe4si5029370pgn.86.2023.07.13.06.04.11; Thu, 13 Jul 2023 06:04:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@t-8ch.de header.s=mail header.b="QSW/8Nr5"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233619AbjGMNBa (ORCPT + 99 others); Thu, 13 Jul 2023 09:01:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229600AbjGMNB3 (ORCPT ); Thu, 13 Jul 2023 09:01:29 -0400 Received: from todd.t-8ch.de (todd.t-8ch.de [IPv6:2a01:4f8:c010:41de::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1A19173B; Thu, 13 Jul 2023 06:01:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=t-8ch.de; s=mail; t=1689253285; bh=yupFDaQfBUJDGYQkoGNGPCB4kwzNRglkYY4WfTsQAWc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QSW/8Nr5ap4LZT45TYM21VhMlU1KpKTveQU/R1eLWHTG7GcTes3d4N4Ugj7E2Dz/X Ov6XIu/ywxhBG9gU8MQm0cIcHEPkyLeeIrSyEsAnPD4BXIvZCGfCE5MuutuFShyQq/ EmfmdBx6FGqYTaw1zz+rlCUm/6Ccq06GWTVd5Ads= Date: Thu, 13 Jul 2023 15:01:24 +0200 From: Thomas =?utf-8?Q?Wei=C3=9Fschuh?= To: Aleksa Sarai Cc: Willy Tarreau , Shuah Khan , Andrew Morton , Christian Brauner , Dave Chinner , xu xin , Al Viro , Stefan Roesch , Zhihao Cheng , "Liam R. Howlett" , Janis Danisevskis , Kees Cook , stable@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH] procfs: block chmod on /proc/thread-self/comm Message-ID: References: <20230713121907.9693-1-cyphar@cyphar.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230713121907.9693-1-cyphar@cyphar.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2023-07-13 22:19:04+1000, Aleksa Sarai wrote: > Due to an oversight in commit 1b3044e39a89 ("procfs: fix pthread > cross-thread naming if !PR_DUMPABLE") in switching from REG to NOD, > chmod operations on /proc/thread-self/comm were no longer blocked as > they are on almost all other procfs files. > > A very similar situation with /proc/self/environ was used to as a root > exploit a long time ago, but procfs has SB_I_NOEXEC so this is simply a > correctness issue. > > Ref: https://lwn.net/Articles/191954/ > Ref: 6d76fa58b050 ("Don't allow chmod() on the /proc// files") > Fixes: 1b3044e39a89 ("procfs: fix pthread cross-thread naming if !PR_DUMPABLE") > Cc: stable@vger.kernel.org # v4.7+ > Signed-off-by: Aleksa Sarai > --- > fs/proc/base.c | 3 ++- > tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++ > 2 files changed, 6 insertions(+), 1 deletion(-) > > diff --git a/fs/proc/base.c b/fs/proc/base.c > index 05452c3b9872..7394229816f3 100644 > --- a/fs/proc/base.c > +++ b/fs/proc/base.c > @@ -3583,7 +3583,8 @@ static int proc_tid_comm_permission(struct mnt_idmap *idmap, > } > > static const struct inode_operations proc_tid_comm_inode_operations = { > - .permission = proc_tid_comm_permission, > + .setattr = proc_setattr, > + .permission = proc_tid_comm_permission, > }; Given that this seems to be a recurring theme a more systematic aproach would help. Something like the following (untested) patch: diff --git a/fs/proc/base.c b/fs/proc/base.c index 05452c3b9872..b90f2e9cda66 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -2649,6 +2649,7 @@ static struct dentry *proc_pident_instantiate(struct dentry *dentry, set_nlink(inode, 2); /* Use getattr to fix if necessary */ if (p->iop) inode->i_op = p->iop; + WARN_ON(!inode->i_op->setattr); if (p->fop) inode->i_fop = p->fop; ei->op = p->op; > /* > diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c > index 486334981e60..08f0969208eb 100644 > --- a/tools/testing/selftests/nolibc/nolibc-test.c > +++ b/tools/testing/selftests/nolibc/nolibc-test.c > @@ -580,6 +580,10 @@ int run_syscall(int min, int max) > CASE_TEST(chmod_net); EXPECT_SYSZR(proc, chmod("/proc/self/net", 0555)); break; > CASE_TEST(chmod_self); EXPECT_SYSER(proc, chmod("/proc/self", 0555), -1, EPERM); break; > CASE_TEST(chown_self); EXPECT_SYSER(proc, chown("/proc/self", 0, 0), -1, EPERM); break; > + CASE_TEST(chmod_self_comm); EXPECT_SYSER(proc, chmod("/proc/self/comm", 0777), -1, EPERM); break; > + CASE_TEST(chmod_tid_comm); EXPECT_SYSER(proc, chmod("/proc/thread-self/comm", 0777), -1, EPERM); break; > + CASE_TEST(chmod_self_environ);EXPECT_SYSER(proc, chmod("/proc/self/environ", 0777), -1, EPERM); break; > + CASE_TEST(chmod_tid_environ); EXPECT_SYSER(proc, chmod("/proc/thread-self/environ", 0777), -1, EPERM); break; I'm not a big fan of this, it abuses the nolibc testsuite to test core kernel functionality. If this needs to be tested explicitly there is hopefully a better place. Those existing tests focus on testing functionality provided by nolibc. The test chmod_net just got removed because it suffered from the same bug as /proc/thread-self/comm. > CASE_TEST(chroot_root); EXPECT_SYSZR(euid0, chroot("/")); break; > CASE_TEST(chroot_blah); EXPECT_SYSER(1, chroot("/proc/self/blah"), -1, ENOENT); break; > CASE_TEST(chroot_exe); EXPECT_SYSER(proc, chroot("/proc/self/exe"), -1, ENOTDIR); break; > -- > 2.41.0 >