Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp504921pxk; Sun, 30 Aug 2020 11:44:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwQH2ZYBt974m8JHHPRUOGXiad/OHvRve7cwSL7Rtmi0HxYQom2LuKY4qtVmjmkny9l94LU X-Received: by 2002:a17:906:b2c3:: with SMTP id cf3mr9106537ejb.387.1598813075662; Sun, 30 Aug 2020 11:44:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598813075; cv=none; d=google.com; s=arc-20160816; b=L76wzLRpmIUiKPkjBYNI2GeFru/EL+fzOM2god59o/JjmCGBq7LvbitDssTkYGVGO1 bYxCVQ89+GGdiGzMXxDOxiPgqx7cwx1DOBSDQIH42QXuQbIMF2vdXmWT5udU8TqgzxnF Q/j2BhYtTR+jkb1RPsQNNLeUIECOKWtQoO37pyqGkPm2WuAiCCqroT8ieGct62QLefW5 ON6LUosgRvkxfFGn8dFeEZ9f2poK1wVGP2jxPc/hVPbwNrwBIiRqXOScHgRkDAHOR6vu pzI2FQDlpq+QNrHbjs1sWa1DXFlnFN9E3oP9yJYs8QSqGmafXapiZC8Xax4bDYXhIMpg ZcKQ== 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; bh=wEecNW0dpN66d9wWVHJMOJJR+KPUSykQorDMQpG0c8I=; b=fJEPuxLhuNzyfZIKZEd67ExZ3zfTEjYXZ+RnNe3AKRW1lSETAFE/t+1p6hB6pCawWQ uv8PLI+6Y/M01NlHPsByEIi/Lgr4fZlV7u34d3WTxM3RFWih7sx0ApIPz7kbAwLQGeHY Gd8pdNVocuDrRi3ybi5kQ5r3J8MDIAHikktzYAeO+QyVCyRSMnH3iT+ixveZwxcOaaf0 k05EOYA/oTrTKgsVmYD6pqct2kSP6mnWXvp24xgweJXwwqB7aZVIt2mrpAkZpVXSnu1/ Z0EjggGBfObKH72hkza0GO8f1QcEQ9Ws0MIm1O6300FgYTOIE/rNscAK/cLce7F8/N49 b1Lw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s2si4020544ejs.269.2020.08.30.11.44.12; Sun, 30 Aug 2020 11:44:35 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726396AbgH3Snj (ORCPT + 99 others); Sun, 30 Aug 2020 14:43:39 -0400 Received: from brightrain.aerifal.cx ([216.12.86.13]:48246 "EHLO brightrain.aerifal.cx" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726178AbgH3Sni (ORCPT ); Sun, 30 Aug 2020 14:43:38 -0400 Date: Sun, 30 Aug 2020 14:43:36 -0400 From: Rich Felker To: Jann Horn Cc: linux-fsdevel , kernel list , Linux API , Alexander Viro Subject: Re: [RESEND PATCH] vfs: add RWF_NOAPPEND flag for pwritev2 Message-ID: <20200830184334.GE3265@brightrain.aerifal.cx> References: <20200829020002.GC3265@brightrain.aerifal.cx> <20200830163657.GD3265@brightrain.aerifal.cx> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: > > On Sun, Aug 30, 2020 at 05:05:45PM +0200, Jann Horn wrote: > > > On Sat, Aug 29, 2020 at 4:00 AM Rich Felker wrote: > > > > The pwrite function, originally defined by POSIX (thus the "p"), is > > > > defined to ignore O_APPEND and write at the offset passed as its > > > > argument. However, historically Linux honored O_APPEND if set and > > > > ignored the offset. This cannot be changed due to stability policy, > > > > but is documented in the man page as a bug. > > > > > > > > Now that there's a pwritev2 syscall providing a superset of the pwrite > > > > functionality that has a flags argument, the conforming behavior can > > > > be offered to userspace via a new flag. > [...] > > > Linux enforces the S_APPEND flag (set by "chattr +a") only at open() > > > time, not at write() time: > [...] > > > It seems to me like your patch will permit bypassing S_APPEND by > > > opening an append-only file with O_WRONLY|O_APPEND, then calling > > > pwritev2() with RWF_NOAPPEND? I think you'll have to add an extra > > > check for IS_APPEND() somewhere. > > > > > > > > > One could also argue that if an O_APPEND file descriptor is handed > > > across privilege boundaries, a programmer might reasonably expect that > > > the recipient will not be able to use the file descriptor for > > > non-append writes; if that is not actually true, that should probably > > > be noted in the open.2 manpage, at the end of the description of > > > O_APPEND. > > > > fcntl F_SETFL can remove O_APPEND, so it is not a security boundary. > > I'm not sure how this interacts with S_APPEND; presumably fcntl > > rechecks it. > > Ah, good point, you're right. In fs/fcntl.c: > > 35 static int setfl(int fd, struct file * filp, unsigned long arg) > 36 { > 37 struct inode * inode = file_inode(filp); > 38 int error = 0; > 39 > 40 /* > 41 * O_APPEND cannot be cleared if the file is marked as append-only > 42 * and the file is open for write. > 43 */ > 44 if (((arg ^ filp->f_flags) & O_APPEND) && IS_APPEND(inode)) > 45 return -EPERM; FWIW I think this check is mildly wrong; it seems to disallow *adding* O_APPEND if the file became chattr +a after it was opened. It should probably be changed to only disallow removal. > > 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. Rich