Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp804318pxp; Fri, 11 Mar 2022 15:36:01 -0800 (PST) X-Google-Smtp-Source: ABdhPJzfpTeQS2Kpj/YgJ7zr2hHb6U4vJVXBHV4imOM9v8JK/fn7hf+OpJvVGcsHuJmVrybfOy7c X-Received: by 2002:a17:902:7892:b0:14e:c520:e47d with SMTP id q18-20020a170902789200b0014ec520e47dmr12176536pll.105.1647041761432; Fri, 11 Mar 2022 15:36:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647041761; cv=none; d=google.com; s=arc-20160816; b=DtjEKK8snF351S5PUcbsA0CGU1KBTOUoJVa25T/8e/8Gw2Ey4O8uUpuH+vUqr75EJM sla4vSkMV69O3oLuaMDFjH6/oTPk1o6QXYvww7/BVqL6zMdgPRYpL5GACR0u/v67Qr1p /Al5AJgoTzXUsPE4IXmeH+/cgJYon8xshannJZXeHY3J5MKsjlTjBGXm7LHWQhZqnnzt gbhqKeCLpb46LQH4x561C1oPg2ULayRfKLzbZlOMK+vHMZ4Z9xn0J1ys6Nqz1GzTrWLp 8yo1nzuIYyV4Y7QJ0iKR9/7iS4Tbnt2Y0BX0cGuO8ujL5mesFyZRVNgWqbeN8vURLk0k Cnfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=BQM5s6KARQo6xsMTrgtyjfqQgxp9yGfc0ghCKKcQtf8=; b=nigU23o6bwL9X2g/xXhSYwLgmhfBfEwfA3R6jqrs+daLMU/ADwMm+hANegZC7LfIra Q9Ys+Y36/HPp3lWzd+AwdAotDNu9ECu1SThQynXQi+f4fwVP6FgEdHVXS3XzZfhRTjDG WIF/DYSM04b1KyXOTSUj9pVRutf297eABcAvgVHiaI3II46RBMy/dYMSDZ/1czpYso6B rhryNRn8txcJ7Sy5/F5LD8I+TMrrVh46AjmClMpDotC/ISLsLRVZn2CzYWe5On28/XV6 IuOqyJjoPqjHRsJXqn4N7/J2dNJhU9FruUIu0cR6zFuTArqefsQNCZejj8C6VfkFSmfM NdjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b="rW/V0Cqh"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z22-20020a63d016000000b0037893662558si9070416pgf.271.2022.03.11.15.36.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 15:36:01 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b="rW/V0Cqh"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E127022BEBD; Fri, 11 Mar 2022 14:46:42 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229678AbiCKWrm (ORCPT + 99 others); Fri, 11 Mar 2022 17:47:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbiCKWr2 (ORCPT ); Fri, 11 Mar 2022 17:47:28 -0500 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 704D3203BDB for ; Fri, 11 Mar 2022 14:22:24 -0800 (PST) Received: by mail-ej1-x62a.google.com with SMTP id p15so21946314ejc.7 for ; Fri, 11 Mar 2022 14:22:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore-com.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=BQM5s6KARQo6xsMTrgtyjfqQgxp9yGfc0ghCKKcQtf8=; b=rW/V0Cqhy9qbkUdJ08Dw1HwcmmSatPyby1AlVx9WHUmj9YQZXnZI6RRhui0BKniegl Bp4L5KkXUZfWD6SlN1Mvrh1Lvk3p0ITMLPhbZQCXgqS7Yq+2PZ6lA8nNYPL0qHRDFzT6 5FrpxfTqET6rhn/cJiLvzXJ0XafMN+n1STTLYYqei2QcB891AuBOiL0LtdufoxoieDmv VhrwqolLTex1i3JOkNgCh/2wmS9XMMwGcMlZYG7X2l49KbEfZ6DjIIfZw62E5a+gVdTv OwHdtrKmna54ohmVYQnBKcHjgaGCKQFMWyxt7uCVl4q0dm/OyD+28lS8CyPG2PRdOSWf /nrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=BQM5s6KARQo6xsMTrgtyjfqQgxp9yGfc0ghCKKcQtf8=; b=8Oo0Fey65HWeDHjudJ2H2HoO+1StrmSYdtkZSlKIi3g02cDA93VGh57205iA32aSxd FedTZ2R/b6Xj7oeZKqwnENPuORyIz0LjN2IRTqzSUlFr3DB1D6tRIE8Jm8rBjJGfqSbP BSse7rFBTG/D5aAR3K49rFQZCfQXIGM6NncQLXA1VT5JHJxMuyjOusjM4ixtUFl4AZbD FUd7IWrqr1BuQtD6aMRRfJsmSizkWgmoRkdknUbWl2lmDhJPX90PFFsevFdQFA1CBkvM UNl82GHyaAE3BWxC8GsnyCtNdqQ6WS+oEXz8izNl6yYJfvgpdf2V9EYoLxY7oZIV7UYC qvVQ== X-Gm-Message-State: AOAM531zuz9WRoJ9egeThwJPPk7hqAfRpnjJJEB3zYA1CvMgbZtfqq0y Cdqaqja00UCSU/jy6GaHEKgGxe0df8jNBCulVn5O90IGUg== X-Received: by 2002:a05:6402:d2:b0:413:2e50:d6fd with SMTP id i18-20020a05640200d200b004132e50d6fdmr10938615edu.171.1647036912117; Fri, 11 Mar 2022 14:15:12 -0800 (PST) MIME-Version: 1.0 References: <20220228215935.748017-1-mic@digikod.net> <20220301092232.wh7m3fxbe7hyxmcu@wittgenstein> <8d520529-4d3e-4874-f359-0ead9207cead@canonical.com> In-Reply-To: <8d520529-4d3e-4874-f359-0ead9207cead@canonical.com> From: Paul Moore Date: Fri, 11 Mar 2022 17:15:01 -0500 Message-ID: Subject: Re: [PATCH v1] fs: Fix inconsistent f_mode To: John Johansen , =?UTF-8?B?TWlja2HDq2wgU2FsYcO8bg==?= , Al Viro Cc: Christian Brauner , "Darrick J . Wong" , Eric Paris , James Morris , Kentaro Takeda , Miklos Szeredi , "Serge E . Hallyn" , Stephen Smalley , Steve French , Tetsuo Handa , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-security-module@vger.kernel.org, =?UTF-8?B?TWlja2HDq2wgU2FsYcO8bg==?= , selinux@vger.kernel.org, Casey Schaufler Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Wed, Mar 9, 2022 at 7:36 PM John Johansen wrote: > On 3/9/22 13:31, Paul Moore wrote: > > On Tue, Mar 1, 2022 at 5:15 AM Micka=C3=ABl Sala=C3=BCn wrote: > >> On 01/03/2022 10:22, Christian Brauner wrote: > >>> On Mon, Feb 28, 2022 at 10:59:35PM +0100, Micka=C3=ABl Sala=C3=BCn wr= ote: > >>>> From: Micka=C3=ABl Sala=C3=BCn > >>>> > >>>> While transitionning to ACC_MODE() with commit 5300990c0370 ("Saniti= ze > >>>> f_flags helpers") and then fixing it with commit 6d125529c6cb ("Fix > >>>> ACC_MODE() for real"), we lost an open flags consistency check. Ope= ning > >>>> a file with O_WRONLY | O_RDWR leads to an f_flags containing MAY_REA= D | > >>>> MAY_WRITE (thanks to the ACC_MODE() helper) and an empty f_mode. > >>>> Indeed, the OPEN_FMODE() helper transforms 3 (an incorrect value) to= 0. > >>>> > >>>> Fortunately, vfs_read() and vfs_write() both check for FMODE_READ, o= r > >>>> respectively FMODE_WRITE, and return an EBADF error if it is absent. > >>>> Before commit 5300990c0370 ("Sanitize f_flags helpers"), opening a f= ile > >>>> with O_WRONLY | O_RDWR returned an EINVAL error. Let's restore this= safe > >>>> behavior. > >>> > >>> That specific part seems a bit risky at first glance. Given that the > >>> patch referenced is from 2009 this means we've been allowing O_WRONLY= | > >>> O_RDWR to succeed for almost 13 years now. > >> > >> Yeah, it's an old bug, but we should keep in mind that a file descript= or > >> created with such flags cannot be used to read nor write. However, > >> unfortunately, it can be used for things like ioctl, fstat, chdir=E2= =80=A6 I > >> don't know if there is any user of this trick. > >> > >> Either way, there is an inconsistency between those using ACC_MODE() a= nd > >> those using OPEN_FMODE(). If we decide to take a side for the behavior > >> of one or the other, without denying to create such FD, it could also > >> break security policies. We have to choose what to potentially break= =E2=80=A6 > > > > I'm not really liking the idea that the empty/0 f_mode field leads to > > SELinux doing an ioctl access check as opposed to the expected > > read|write check. Yes, other parts of the code catch the problem, but > > this is bad from a SELinux perspective. Looking quickly at the other > > LSMs, it would appear that other LSMs are affected as well. > > > > If we're not going to fix file::f_mode, the LSMs probably need to > > consider using file::f_flags directly in conjunction with a correct > > OPEN_FMODE() macro (or better yet a small inline function that isn't > > as ugly). > > > yeah, I have to agree The silence on this has been deafening :/ No thoughts on fixing, or not fixing OPEN_FMODE(), Al? At this point I have to assume OPEN_FMODE() isn't changing so I'm going to go ahead with moving SELinux over to file::f_flags. Once I've got something working I'll CC the LSM list on the patches in case the other LSMs want to do something similar. Full disclosure, that might not happen until early-to-mid next week due to the weekend, new kernel expected on Sunday, etc. --=20 paul-moore.com