Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1626729rwb; Fri, 13 Jan 2023 15:16:23 -0800 (PST) X-Google-Smtp-Source: AMrXdXuHYvBIZJ0uuQtb16IQfKZwPzhaaYFBqhO+cRBqRnLodii0tiwQHxjSdDzjNcYxP+/A8cSx X-Received: by 2002:a17:906:434f:b0:7fc:4242:fa1d with SMTP id z15-20020a170906434f00b007fc4242fa1dmr80864043ejm.54.1673651783387; Fri, 13 Jan 2023 15:16:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673651783; cv=none; d=google.com; s=arc-20160816; b=c7Zbhpv3jJxNYUAZ5ofK/FMOOJiSYnCHjN1PI6PmzLAFuv0Yvt78urn8gSbSvrAg/9 5J+xA0yHTuIi7eRFOqMguH4rJ50mlwQTFGNO68gThz43gYh5V271Wp7Gm0ajAleA0fcf pUIrAOSp/dw9l275sI3XKxocGo5SZQgP5eAzH3K10avCKMYtm8YMrInNKuyjQ4T9GpKQ Y+Z5gMG3kDJLgaz44iQg3pNsnBFa/ECa1U26+RQCD8ORoVak+4N10ZcRb+pMNTlDyHBD KXfFILx2Q4dqWRHoeUOdYLKqt51jYbV5aTOJFW3lMMSRx05dCSunjzIv0klkWNaR8H35 KaXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=iwz7mFj6BnnAFkpB1WWVGVKEvGEgqsYMT8OTDqMBOOc=; b=iMsIMFjwh9YRhSpcNBxlMTUv6EDEbFQAE0ABedxRyG9P8FFq7704WcRs2ovJBAxF2j oPj/rSi/ka9YY0a57ax+KxAklU6RScalOjqao9v6CMuYdX+QMEbqe5jE0lK6QJzV0Xp8 zX8uv7IjW/dqXqbpi9EV5ah0+juB4oFvEumFVk4B7/0oqdIYPO91Sp+QCQjowordiX/L arcZcYVnwWFIGU3SNkSQm4KErs5n3bbvhWPuZCH9QSJRIj5PsL5J6pHOMg/MJmMuMiDb z+SFAFArhuTouW5mKBtSATeqpB6k4/0wYVIeG6Vt5nFBpe1U9CcwHpgLTYXAUvBZr53w PVeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=S5supkgb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oz11-20020a1709077d8b00b0086c92fed1dfsi910851ejc.979.2023.01.13.15.16.10; Fri, 13 Jan 2023 15:16:23 -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=@infradead.org header.s=bombadil.20210309 header.b=S5supkgb; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231338AbjAMXHD (ORCPT + 52 others); Fri, 13 Jan 2023 18:07:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53420 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231221AbjAMXHB (ORCPT ); Fri, 13 Jan 2023 18:07:01 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35FB02AE6; Fri, 13 Jan 2023 15:06:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=iwz7mFj6BnnAFkpB1WWVGVKEvGEgqsYMT8OTDqMBOOc=; b=S5supkgbtZvpM5usiJROXh2SMd Y1hgUhSckjLRWnEj5HAZ28xKY2/jO40aad8NX8SKFWSCGAGe9KDs1BwfBsuo5kkXW+s94bGtLGHLI tCwn3nTKTH6Y54z00u1h2FEamqJJ9FozrXUhLCT+bbmOAPmvO4MpU3yC4BZZPuqCJuQpnxv4XE4ri i/ADfdDAjavLauay0iZr1J1GtFopf3WPbsKebPod2u5+ZdkCp2KfKD1UgttFwmQjf6xHk24kVgjeO ydAAQcarw67L37NRtPQlurvEl5LBfoxJmbuhhgEvzjMnppCsrL1pAdipAe6/gxO2dsi8h/TCCtzjr jxA615hw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1pGT8N-004mqa-Md; Fri, 13 Jan 2023 23:06:51 +0000 Date: Fri, 13 Jan 2023 15:06:51 -0800 From: Luis Chamberlain To: Vegard Nossum Cc: Petr Pavlu , Petr Mladek , prarit@redhat.com, david@redhat.com, mwilck@suse.com, linux-kernel@vger.kernel.org, Thadeu Lima de Souza Cascardo , Serge Hallyn , Eric Biederman , Kees Cook , linux-hardening@vger.kernel.org, linux-modules@vger.kernel.org, John Haxby , Jann Horn Subject: Re: [PATCH v3] kmod: harden user namespaces with new kernel.ns_modules_allowed sysctl Message-ID: References: <20230112131911.7684-1-vegard.nossum@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: Luis Chamberlain X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE 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 Thu, Jan 12, 2023 at 10:53:07PM +0100, Vegard Nossum wrote: > On 1/12/23 19:45, Luis Chamberlain wrote: > > On Thu, Jan 12, 2023 at 02:19:11PM +0100, Vegard Nossum wrote: > > > +ns_modules_allowed > > > +================== > > > + > > > +Control whether processes may trigger module loading inside a user namespace. > > > > This is false documentation. The place it is trying to protect simply > > prevents trying to call modprobe for auto-loading within the kernel. > > I don't think this is false -- but yes, this only protects module > auto-loading in user namespaces; all auto-loading calls within the > kernel should be going through this __request_module() -> modprobe path. > > init_module()/finit_module(), the mechanism used by modprobe, are > themselves already restricted inside user namespaces, see below. The documentation should be clear about the exact nature of what mechanism is prevented to load. > > > + /* > > > + * Disallow module loading if we're in a user namespace. > > > + */ > > > + if (current_user_ns() != &init_user_ns && > > > + !sysctl_ns_modules_allowed) { > > > + pr_warn_ratelimited("request_module: pid %d (%s) in user namespace requested kernel module %s; denied due to kernel.ns_modules_allowed sysctl\n", > > > + task_pid_nr(current), current->comm, module_name); > > > + return -EPERM; > > > + } > > > + > > > if (atomic_dec_if_positive(&kmod_concurrent_max) < 0) { > > > pr_warn_ratelimited("request_module: kmod_concurrent_max (%u) close to 0 (max_modprobes: %u), for module %s, throttling...", > > > atomic_read(&kmod_concurrent_max), > > > > Have you seen what call_modprobe() does? > > Yes. > > > This is just a limitting the auto-loading through calling modprobe. > > If the concern is to load modules wouldn't you be better off just > > putting a stop gap at finit_module() which actually receives the > > load attempt from modprobe? Ie, an evil namespace, if it has access > > to /sbin/modprobe could simply just try calling /sbin/modprobe on its > > own. > > No. > > Root inside a user namespace can't call finit_module() as it won't have > the necessary capabilities in the init namespace, see may_init_module(). I think the documentation to this knob you are adding should explain this as well to give a bette context as to why it is useful. And if may_init_module() suffices, why not just add a may_init_module() instead of your check? Why would we allow a successful call to modprobe with your sysctl but end up having it ignored in the end by finit_module()? If what is being allowed here is to overcome may_init_module() CAP_SYS_MODULE check by using call_modprobe() on the user namespace the commit log should just mention this, and mention that by design user namespaces have never been intended to allow loading modules, even if they somehow end up triggering auto-loading of modules via request_module(). If this is true then currently we are enabling auto-loading as a mistake, and the sysctl is only valuable to prevent regressions with existing behaviour which should have been disabled from the start. Is there an example successful exploit which takes avantage of having a user namespace auto-load a kernel module and that triggers a security flaw? What's an example of one? > > What this *could* do is race to force a failure on some other *real* > > modprobe request we do wish to honor when the above kmod kmod_concurrent_max > > is triggered. > > How? My new check is done before the kmod_concurrent_max check/critical > section... the check doesn't cause any more modprobe requests to happen > in the first place, the only thing it can do is make them exit early. > There is no way my patch can make this worse. You are missing my point. My point is that abuse in general over having a user namespace calling modprobe could cause harm that it can possibly Denial-of-Service valid requests. If user namespaces should never have been allowed to even auto-load the commit log should mention that. > > So in terms of justification, this commit log needs a bit more work as I > > just can't see how this alone is fixing any CVE. > > [...] > > > So let's take a step back and think this through. What exaclty and why > > would this commit fix *any* security issue? Itemizing CVEs won't cut it. > > I can include my explanations above in the changelog if you think that > will help. Answering the above questions would help. Luis