Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp993255pxp; Wed, 9 Mar 2022 17:58:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJwzQvZkI70YRKx5MgJLjppGA1y2fgJETmvb7QD0fcpYCpURSN+fLflz0A29XdHapOkqPDaS X-Received: by 2002:a17:907:7659:b0:6da:a62f:8c1d with SMTP id kj25-20020a170907765900b006daa62f8c1dmr2208461ejc.453.1646877482981; Wed, 09 Mar 2022 17:58:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646877482; cv=none; d=google.com; s=arc-20160816; b=FCu9QItVFelWLQGc99i7ueyqaFZlBJ5Fkmhva6v2esSouMegaInYYtTefqIAou6Gvs XgHNnTanMgOlZNhNSC1KyAew5UrH3OEdm49JMFST9SX+SDGqNnVu8/I8KwT+sq5W8eU0 HxuU1/kU/TF92pDNxf3hNBjDGyBdbUZIocFsJ0WoQ9ZQB/no9zmSbtbByNFbOnQ4Zdgo RV8nuZCn7FT404nbr9PAED8XdvKRjC3dedhZRI5V/qufq1Qi1F1G3MC7G+iBWPYvxzXP S+5Wbr3/EAKEj7Rf38INz/BFIiZCHXKZNCzTM7HbMR1uq5scMaTN57KRh4Ndy3YNXQb5 vr5A== 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=ZHkvHMA1I6eh/YZqJlZmNXRy1yBzafqBEJKVUwInNkU=; b=BVupffwE/OmxVlpiLkqnCHrOHqLhYXl7yLGDObv7H6JoY4CoG/xrZ/QJ6MgJWOvtYv wIsW2XNsq2CSa/KfnQT/rmWHaLW9lPBZ9TqCoz8XQM1ivPalZ2K0B90F+AsGZuJNkyB2 G+YSMd58anstd7s7hlvElUlGZAZiUe4EVS05DSjrkKclBkQ974VAtKztXtAXVNXN338f EUly8Vo1vVkWcYKeZE1h5vvr7OUX4pP/oHv4Kpix9N4Z0fsSBSw0LgKKuQ4vkJy5XQe0 vPlRT/HIDKHIesSu5vKwGCyFywBg3cYii8EFFbS4/MkuyyvJHIRV1/64XYwQ6qGx52SY SjNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b="T/pixWr7"; 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 sa13-20020a1709076d0d00b006d0aae3cb48si2189689ejc.270.2022.03.09.17.57.40; Wed, 09 Mar 2022 17:58:02 -0800 (PST) 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=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b="T/pixWr7"; 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 S233344AbiCIVdL (ORCPT + 99 others); Wed, 9 Mar 2022 16:33:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236791AbiCIVdK (ORCPT ); Wed, 9 Mar 2022 16:33:10 -0500 Received: from mail-ed1-x534.google.com (mail-ed1-x534.google.com [IPv6:2a00:1450:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8729611D797 for ; Wed, 9 Mar 2022 13:32:09 -0800 (PST) Received: by mail-ed1-x534.google.com with SMTP id g3so4586610edu.1 for ; Wed, 09 Mar 2022 13:32:09 -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=ZHkvHMA1I6eh/YZqJlZmNXRy1yBzafqBEJKVUwInNkU=; b=T/pixWr78MTQFIr0gZocxDvBgcUg8EdHrLBsGWDk2HasstXtU8yYvGSGdTEw8/CkBA p6xuAsgcGRciwA4yRltBP8eAhwCOgupho6O3XUF1RdZqzSHnIDtBStqZ6tpGLu6SBWRe p+Y2GF7wUVcPkKSKXNtNjFtJ64aFK78dPs5S3NKndWJ82s1AuAITJ5phuhVQCuk89zoY JJZijUXSZSZI4XD5jXTY7tiyn2SRbA1qcTNtz2f+WIzaEAhd2n8jreps3ybBJWe88Jq5 5su8hAfUj0zUsNjXpL3t7X4Oxm+QgqSdxh10eIxKu7Z632mzJivSvC465shwR3HzKH2/ AUaQ== 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=ZHkvHMA1I6eh/YZqJlZmNXRy1yBzafqBEJKVUwInNkU=; b=Z/8Xq09OEmOqxPHLDVk7LzIVCbFzjAr9pU98YH5L+51QV9kMmdnO3kDKOFYUW0w5EP c3tu3y3nJvIjBF2RbWI9cnJmTAqCOSkeXnoYxMs1vxXjQXC7WrWWXrpz7UAS31xLQzGr 5p47FQdiiigRO4qXK+6TVSMOuCANsx8gVcU5LcBMOygQhdihMkYm/NiKvEvUc7zRblNv 4k4zXq7jQnSbDRRTDKoF1tzV0e6yEeK0i+iO4UOQapqJTfntQdxXCISvbyM2pvM3DHCa DuNQDbuEMvFRCdDGK9tOl/c4zWKHJtPcaLOYW1iCHTstXRQI7j7qfHKZI/b2HvCKFQNp w+Zg== X-Gm-Message-State: AOAM5318FHHEMsjjYi/fynas6OfeP4KI8ElXPHqdCA2tugmX6L+ZputL jmtURf4PAhVz0qfzLSGPf2T4l9cS6QCbvrcXaPiV X-Received: by 2002:a05:6402:90c:b0:415:d340:4ae2 with SMTP id g12-20020a056402090c00b00415d3404ae2mr1394660edz.331.1646861527956; Wed, 09 Mar 2022 13:32:07 -0800 (PST) MIME-Version: 1.0 References: <20220228215935.748017-1-mic@digikod.net> <20220301092232.wh7m3fxbe7hyxmcu@wittgenstein> In-Reply-To: From: Paul Moore Date: Wed, 9 Mar 2022 16:31:57 -0500 Message-ID: Subject: Re: [PATCH v1] fs: Fix inconsistent f_mode To: =?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 , John Johansen 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,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Tue, Mar 1, 2022 at 5:15 AM Micka=C3=ABl Sala=C3=BCn w= rote: > 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 wrot= e: > >> From: Micka=C3=ABl Sala=C3=BCn > >> > >> While transitionning to ACC_MODE() with commit 5300990c0370 ("Sanitize > >> f_flags helpers") and then fixing it with commit 6d125529c6cb ("Fix > >> ACC_MODE() for real"), we lost an open flags consistency check. Openi= ng > >> a file with O_WRONLY | O_RDWR leads to an f_flags containing MAY_READ = | > >> 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, or > >> respectively FMODE_WRITE, and return an EBADF error if it is absent. > >> Before commit 5300990c0370 ("Sanitize f_flags helpers"), opening a fil= e > >> with O_WRONLY | O_RDWR returned an EINVAL error. Let's restore this s= afe > >> 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 descriptor > 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() and > 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). --=20 paul-moore.com