Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp958964pxk; Mon, 31 Aug 2020 06:15:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2M4E3qXczCLStTzV42/MCTAfQmbNEVwBmaZtTbx3td5fOeBXNzZikSV/UtgR9S/gD7qB3 X-Received: by 2002:aa7:d593:: with SMTP id r19mr1250443edq.331.1598879714548; Mon, 31 Aug 2020 06:15:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598879714; cv=none; d=google.com; s=arc-20160816; b=xVuzb2ar4pc6p62l6lP9v0UX0uHaj3M6EYPG7naB6K/TuGbewNCyk4UXXJnXQ7RiFR /fGlpJcdibvxEI6Ez0tPw9DM4H+giM4LBDE5ds1IW6xcd8xb1K8mIH09/w3tUNJpd/oX /osO2gRnGCqXQHhsMXOgmB3zb3WoSxZ51SHLmWBMfh2MOx5RJfRbeBM/Wp9v7a90j5Hu 9H8xk08GCCDdWSwj4ujsdd0Czwp9wS4OInnJOxB9F9DRTEchUQnG3NUhkUAc9CVu8E7n 185MgZmgh1kj57B3xwJadkzEuJgg+cHMT9STXMlAJv1vJzkjsyZ831aLVoNNUw363SxE R0fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=2ciTA6uMUrkdw0DiQy92AHLmokEL0/SHTSQBqrxd/0g=; b=OvbEsa/lyRs88+vz6E1ruKOuVyrEXS0fzocWH/NW111p6KW8c08Lxx4OV9XBRoP/Wx ArLXjYNxASG0KOhkAK3grbXkh7c3GXi9V/kn2aGKIzoJH64N+8bUtd9Lj0hKUNKeWK9T jpVzseeIBaWaw3iiCPt5Kfsz/W4JURbijn/8ylaZxHOU/ABh5+SMoXworu+4jz5o3CaL 3TZ4IfpQpDQVXtLylmVy1rQsCo/M46G3eNfiIP8u2DqYdnGPB4PaqhoSv35b3Ockx9UR 4a0RloGjkiwZ8B5xTWaM1kVvrWvrs/3OAhmVj8oXi8uhXd4ZY714PRPB7zNKrhpGxg1z B3gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="bc/XIaUM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id sd28si3626182ejb.302.2020.08.31.06.14.50; Mon, 31 Aug 2020 06:15:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="bc/XIaUM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726468AbgHaNNc (ORCPT + 99 others); Mon, 31 Aug 2020 09:13:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726515AbgHaNNS (ORCPT ); Mon, 31 Aug 2020 09:13:18 -0400 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93693C061573 for ; Mon, 31 Aug 2020 06:12:58 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id c15so3474629lfi.3 for ; Mon, 31 Aug 2020 06:12:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2ciTA6uMUrkdw0DiQy92AHLmokEL0/SHTSQBqrxd/0g=; b=bc/XIaUM/sXaX5cfvs4s9Q8j4AY3FCdVMGe0yfwu4jyocWRsUrPnOr8YRY6H2PofBb MpIF9at/ZHXTwvisoewlZHrWr61kpcZGDTrNETb8dUSeJiZEHa1V4gyykMkAadWvYiei sHs16F7KZ+zj9mA9N1GN25okbhoWmUgAGAE/bszczun5GNlcFBvzCVVo0nFiP7b9pmq8 pEDq7UjQKpeHw3dAo9MXLdnDebsSIUAcRQ5Qre38y6XYbev+uQKJd1SwcmVgNJP9bIEi HpJVCIsPZfmpERBwWeeMJnjJA5AULicS3THvs9SF6BJnHJ+dyRCdUX68ub379lMnYA0r /EEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2ciTA6uMUrkdw0DiQy92AHLmokEL0/SHTSQBqrxd/0g=; b=FPN2Giov5U68ATOQ1v0bCzBo4sr6GIzEDOoBC0OcbWTHBaZUeqd3I8MfzYgtukxqCw 07EX8mBiyToNdFXryJLqmTBpVlFNH90Myoi1O5tEMd0CDmn0fsaDm1BcIiRCJ5e0jfyF lJAwz/zQiPx4wQgnJnsF2vc6KtI+bcqcPPNqkhJgY2E4EL7tMDWFEyvOIkXGizMk2O5/ R5cExrQBrdj8wzvrz1q+hXX1KMwuTN47RmV7U0tTEtcw1ueMNaSTANfPT4m2Xbhg5R+4 2QMehKNXKLrCzs/TCar37R8com0ChN2MFNlN45lMJzK3HCttfD0+swlfl5ApsiIlX9BM bZFA== X-Gm-Message-State: AOAM532/ooT3iB2fyTwu2pUF/ta7+BKftFmVsMcibxaX9ZonCvnjsEMn JsYn2lZ8gh5xCzvqB+Mh303+IzBCr6wD4X1MXEVRMA== X-Received: by 2002:a19:418a:: with SMTP id o132mr691360lfa.63.1598879576685; Mon, 31 Aug 2020 06:12:56 -0700 (PDT) MIME-Version: 1.0 References: <20200829020002.GC3265@brightrain.aerifal.cx> <20200830163657.GD3265@brightrain.aerifal.cx> <20200830184334.GE3265@brightrain.aerifal.cx> <20200830200029.GF3265@brightrain.aerifal.cx> <20200831014633.GJ3265@brightrain.aerifal.cx> <20200831125707.GM3265@brightrain.aerifal.cx> In-Reply-To: <20200831125707.GM3265@brightrain.aerifal.cx> From: Jann Horn Date: Mon, 31 Aug 2020 15:12:30 +0200 Message-ID: Subject: Re: [RESEND PATCH] vfs: add RWF_NOAPPEND flag for pwritev2 To: Rich Felker Cc: linux-fsdevel , kernel list , Linux API , Alexander Viro Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 31, 2020 at 2:57 PM Rich Felker wrote: > On Mon, Aug 31, 2020 at 11:15:57AM +0200, Jann Horn wrote: > > On Mon, Aug 31, 2020 at 3:46 AM Rich Felker wrote: > > > On Mon, Aug 31, 2020 at 03:15:04AM +0200, Jann Horn wrote: > > > > On Sun, Aug 30, 2020 at 10:00 PM Rich Felker wrote: > > > > > On Sun, Aug 30, 2020 at 09:02:31PM +0200, Jann Horn wrote: > > > > > > On Sun, Aug 30, 2020 at 8:43 PM Rich Felker wrote: > > > > > > > On Sun, Aug 30, 2020 at 08:31:36PM +0200, Jann Horn wrote: > > > > > > > > On Sun, Aug 30, 2020 at 6:36 PM Rich Felker wrote: > > > > > > > > > So just checking IS_APPEND in the code paths used by > > > > > > > > > pwritev2 (and erroring out rather than silently writing output at the > > > > > > > > > wrong place) should suffice to preserve all existing security > > > > > > > > > invariants. > > > > > > > > > > > > > > > > Makes sense. > > > > > > > > > > > > > > There are 3 places where kiocb_set_rw_flags is called with flags that > > > > > > > seem to be controlled by userspace: aio.c, io_uring.c, and > > > > > > > read_write.c. Presumably each needs to EPERM out on RWF_NOAPPEND if > > > > > > > the underlying inode is S_APPEND. To avoid repeating the same logic in > > > > > > > an error-prone way, should kiocb_set_rw_flags's signature be updated > > > > > > > to take the filp so that it can obtain the inode and check IS_APPEND > > > > > > > before accepting RWF_NOAPPEND? It's inline so this should avoid > > > > > > > actually loading anything except in the codepath where > > > > > > > flags&RWF_NOAPPEND is nonzero. > > > > > > > > > > > > You can get the file pointer from ki->ki_filp. See the RWF_NOWAIT > > > > > > branch of kiocb_set_rw_flags(). > > > > > > > > > > Thanks. I should have looked for that. OK, so a fixup like this on top > > > > > of the existing patch? > > > > > > > > > > diff --git a/include/linux/fs.h b/include/linux/fs.h > > > > > index 473289bff4c6..674131e8d139 100644 > > > > > --- a/include/linux/fs.h > > > > > +++ b/include/linux/fs.h > > > > > @@ -3457,8 +3457,11 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags) > > > > > ki->ki_flags |= (IOCB_DSYNC | IOCB_SYNC); > > > > > if (flags & RWF_APPEND) > > > > > ki->ki_flags |= IOCB_APPEND; > > > > > - if (flags & RWF_NOAPPEND) > > > > > + if (flags & RWF_NOAPPEND) { > > > > > + if (IS_APPEND(file_inode(ki->ki_filp))) > > > > > + return -EPERM; > > > > > ki->ki_flags &= ~IOCB_APPEND; > > > > > + } > > > > > return 0; > > > > > } > > > > > > > > > > If this is good I'll submit a v2 as the above squashed with the > > > > > original patch. > > > > > > > > Looks good to me. > > > > > > Actually it's not quite. I think it should be: > > > > > > if ((flags & RWF_NOAPPEND) & (ki->ki_flags & IOCB_APPEND)) { > > > if (IS_APPEND(file_inode(ki->ki_filp))) > > > return -EPERM; > > > ki->ki_flags &= ~IOCB_APPEND; > > > } > > > > > > i.e. don't refuse RWF_NOAPPEND on a file that was already successfully > > > opened without O_APPEND that only subsequently got chattr +a. The > > > permission check should only be done if it's overriding the default > > > action for how the file is open. > > > > > > This is actually related to the fcntl corner case mentioned before. [...] > While reparing this I rebased against Linus's tree, and found > conflicts with 1752f0adea98ef85 which were easy to resolve. > Unfortunately the same improvement made in that commit does not work > for the new RWF_NOAPPEND, since it needs to inspect and mask bits off > the original ki_flags, not the local set of added flags, but the > penalty should be isolated to this branch. I'm not opposed to adding > unlikely() around it if you think that would help codegen for the > common cases. > > Alternatively, kiocb_flags could be initialized to ki->ki_flags, with > assignment-back in place of |= at the end of the function. This might > be more elegant but I'm not sure if the emitted code would improve. Presumably RWF_NOAPPEND would be somewhat rare, and a simple comparison and not-taken branch should be really cheap? I'm not really concerned about it. I guess you can CC the author of that patch on your v2.