Received: by 2002:a05:6512:e85:0:0:0:0 with SMTP id bi5csp134732lfb; Thu, 23 Jun 2022 20:56:27 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vT6FI6YYAEdVkpiGepUyDLrwrFKw0yMUKHZLiSSsl9pVk7aMotRWKg5GRpFtNlz2IL68gZ X-Received: by 2002:aa7:c1c7:0:b0:435:5cb2:c202 with SMTP id d7-20020aa7c1c7000000b004355cb2c202mr15166761edp.10.1656042987651; Thu, 23 Jun 2022 20:56:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656042987; cv=none; d=google.com; s=arc-20160816; b=nXMmxaW6eljt2SO4/UrwTgFg/sX+BgIlKRGnA7f+9O1db6hGXTYvb9msi2MW35kNI0 +sb60lvXxyE+jljH5U87yd14x9Xvitl3ivB6U34lbMWBDWlps1PcqYRoQF3pM90Z1bjd UJ5dBEy3FXOmz1adXX1nLo6n9CGHy9gUqkJ3tqLWGjwxND/PO+GVXzqMDwxhq40fQCA/ zzH1/iPR3lEgdRy5eWov0VmPWQ7aUNe0jDRjyW2EWgWUY7iEQS7JLeqM7HV62jEkhSCx yrEULaZ4GwAh0x3ZN/5qyA0gKr/OHBtsYqYEFcT2UeecqINRxkCskxjbszkNsxoz8EXO 4gMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=nkA12o8J/G8WC/x2ZjqUQFWdUod71jTMrhuVlQ+gIfc=; b=oKlUfh8SkygJxcyPyzRrSoqtZgiGLHdO7AY5EOpIjN9uo0yaPyg6gQc+jC5hUZLg8w X/+lx+pidxnUOghKxtg7n/1FdQvHNFBDnckG8KBs7Llx/TYCQ4y8yvrOEEbhdhhdG6eZ FtcqslRoI9zMUfsukEdmhsVcIATL4RMng4oNSVrvRUUJb9SCs0zEvKiUR/vMXPwlRnrO EYUhCtnDTaFZ/4GB4IPoNK+rHm7y63zGLNF1jGbj8F+m7HVUFU0WkGdzKmymOgFNHbqs ROSBHmYwdQXN21wCEiokwiqr7sJaeb9HOfh7pTlaS38r6Mo3mpMs2tYCeND/iWwNdvu3 E59Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20210112.gappssmtp.com header.s=20210112 header.b=cB+o0n9F; 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 ho17-20020a1709070e9100b006f3a3c628c8si1310309ejc.781.2022.06.23.20.56.03; Thu, 23 Jun 2022 20:56:27 -0700 (PDT) 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=cB+o0n9F; 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 S229879AbiFXDVw (ORCPT + 99 others); Thu, 23 Jun 2022 23:21:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbiFXDVv (ORCPT ); Thu, 23 Jun 2022 23:21:51 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D97762710 for ; Thu, 23 Jun 2022 20:21:50 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id o16so1343361wra.4 for ; Thu, 23 Jun 2022 20:21:50 -0700 (PDT) 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; bh=nkA12o8J/G8WC/x2ZjqUQFWdUod71jTMrhuVlQ+gIfc=; b=cB+o0n9FydEJuF7kdgUBwLms6sg/mL07v5Fm4wCK5oaeo52McxaVgaMqRXxn82FBYl YxRmxsrCOhxOyr1v5satohMH1/s+X6ZRb+NJZsgIthjEXz6y1+tTqFm9WOGZz6rr3SU/ zxWAbi5cozSEypGIO33gbc+ffLdv5Sp3E+DAETn5U87nrlBRouptfEhknKi4Q0S6RWgc cpkb+qF9DXJ54Hj4sYi+NOHN1ZYPWYZfkUTQBbwufaxWiz9mBlye7YYbqmcYiM9KFWbU aakhigOid5o+64hSNKle7XLMwbisINZEz+pZtDge+dsqY4Z+lGnJ08gdtXl5Xz5lLx1D U6dA== 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; bh=nkA12o8J/G8WC/x2ZjqUQFWdUod71jTMrhuVlQ+gIfc=; b=wWpwUllZcirs3MRgKgw+j/llynxMBxYe2olbDCMRrZUq/+8tP16iIjbUwXmKLQD83k H3TqiBBWkmXBD5oFs17gG71hc4EGcUUjQicxY1NGmu+zM6knSHwirp4jsA58N2EpZCgJ guM6hw/nB4LUGZA/dGgejEWl6vVrPAt5HvIvLgkNbddRS4g+dm/K1vw0BUmlPTaZAUW/ hUY/X3EXFirsEgxN9DALZd4j4nu0O8eaX2LXm3cAMWH0DoxWY6y68kenb94ydb5g8Uxh 07YyT2A24i0XyqMRnw/ayZdVmNXBlxQVAtq5PI9iMDgOL7GsSjGaVqq16P6ejpi64HbF +ndg== X-Gm-Message-State: AJIora/wjE6nGZnW7RTwGKy/HXv153H0qqGJ6qZa6AmPX2R4WXipau0t p/KoDlvtbL18VICqHchrTIj/IAaRpdlYNvSctzne X-Received: by 2002:a5d:4848:0:b0:21b:8cda:5747 with SMTP id n8-20020a5d4848000000b0021b8cda5747mr11337757wrs.483.1656040908556; Thu, 23 Jun 2022 20:21:48 -0700 (PDT) MIME-Version: 1.0 References: <20220621233939.993579-1-fred@cloudflare.com> In-Reply-To: From: Paul Moore Date: Thu, 23 Jun 2022 23:21:37 -0400 Message-ID: Subject: Re: [PATCH 0/2] Introduce security_create_user_ns() To: Frederick Lawler Cc: Casey Schaufler , kpsingh@kernel.org, revest@chromium.org, jackmanb@chromium.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, kafai@fb.com, songliubraving@fb.com, yhs@fb.com, john.fastabend@gmail.com, jmorris@namei.org, serge@hallyn.com, bpf@vger.kernel.org, linux-security-module@vger.kernel.org, brauner@kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-team@cloudflare.com Content-Type: text/plain; charset="UTF-8" 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=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 Wed, Jun 22, 2022 at 10:24 AM Frederick Lawler wrote: > On 6/21/22 7:19 PM, Casey Schaufler wrote: > > On 6/21/2022 4:39 PM, Frederick Lawler wrote: > >> While creating a LSM BPF MAC policy to block user namespace creation, we > >> used the LSM cred_prepare hook because that is the closest hook to > >> prevent > >> a call to create_user_ns(). > >> > >> The calls look something like this: > >> > >> cred = prepare_creds() > >> security_prepare_creds() > >> call_int_hook(cred_prepare, ... > >> if (cred) > >> create_user_ns(cred) > >> > >> We noticed that error codes were not propagated from this hook and > >> introduced a patch [1] to propagate those errors. > >> > >> The discussion notes that security_prepare_creds() > >> is not appropriate for MAC policies, and instead the hook is > >> meant for LSM authors to prepare credentials for mutation. [2] > >> > >> Ultimately, we concluded that a better course of action is to introduce > >> a new security hook for LSM authors. [3] > >> > >> This patch set first introduces a new security_create_user_ns() function > >> and create_user_ns LSM hook, then marks the hook as sleepable in BPF. > > > > Why restrict this hook to user namespaces? It seems that an LSM that > > chooses to preform controls on user namespaces may want to do so for > > network namespaces as well. > > IIRC, CLONE_NEWUSER is the only namespace flag that does not require > CAP_SYS_ADMIN. There is a security use case to prevent this namespace > from being created within an unprivileged environment. I'm not opposed > to a more generic hook, but I don't currently have a use case to block > any others. We can also say the same is true for the other namespaces: > add this generic security function to these too. > > I'm curious what others think about this too. While user namespaces are obviously one of the more significant namespaces from a security perspective, I do think it seems reasonable that the LSMs could benefit from additional namespace creation hooks. However, I don't think we need to do all of them at once, starting with a userns hook seems okay to me. I also think that using the same LSM hook as an access control point for all of the different namespaces would be a mistake. At the very least we would need to pass a flag or some form of context to the hook to indicate which new namespace(s) are being requested and I fear that is a problem waiting to happen. That isn't to say someone couldn't mistakenly call the security_create_user_ns(...) from the mount namespace code, but I suspect that is much easier to identify as wrong than the equivalent security_create_ns(USER, ...). We also should acknowledge that while in most cases the current task's credentials are probably sufficient to make any LSM access control decisions around namespace creation, it's possible that for some namespaces we would need to pass additional, namespace specific info to the LSM. With a shared LSM hook this could become rather awkward. > > Also, the hook seems backwards. You should > > decide if the creation of the namespace is allowed before you create it. > > Passing the new namespace to a function that checks to see creating a > > namespace is allowed doesn't make a lot off sense. > > I think having more context to a security hook is a good thing. This is one of the reasons why I usually like to see at least one LSM implementation to go along with every new/modified hook. The implementation forces you to think about what information is necessary to perform a basic access control decision; sometimes it isn't always obvious until you have to write the access control :) [aside: If you would like to explore the SELinux implementation let me know, I'm happy to work with you on this. I suspect Casey and the other LSM maintainers would also be willing to do the same for their LSMs.] In this particular case I think the calling task's credentials are generally all that is needed. You mention that the newly created namespace would be helpful, so I'll ask: what info in the new ns do you believe would be helpful in making an access decision about its creation? Once we've sorted that we can make a better decision about the hook placement, but right now my gut feeling is that we only need to pass the task's creds, and I think placing the hook right after the UID/GID mapping check (before the new ns allocation) would be the best spot. -- paul-moore.com