From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A612C43334 for ; Tue, 14 Jun 2022 18:59:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF7666B0078; Tue, 14 Jun 2022 14:59:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C7D066B0073; Tue, 14 Jun 2022 14:59:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B241F6B0075; Tue, 14 Jun 2022 14:59:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9C3896B0072 for ; Tue, 14 Jun 2022 14:59:14 -0400 (EDT) Received: from smtpin31.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 696B834DD9 for ; Tue, 14 Jun 2022 18:59:14 +0000 (UTC) X-FDA: 79577754228.31.6E0518A Received: from mail-oa1-f49.google.com (mail-oa1-f49.google.com [209.85.160.49]) by imf12.hostedemail.com (Postfix) with ESMTP id 872B6400A7 for ; Tue, 14 Jun 2022 18:59:13 +0000 (UTC) Received: by mail-oa1-f49.google.com with SMTP id 586e51a60fabf-1016409cf0bso2544908fac.12 for ; Tue, 14 Jun 2022 11:59:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :cc:references:from:in-reply-to:content-transfer-encoding; bh=P/zvL47bzMX9zv6R9Iu19Vam9aHyZ/GbOGpqtXAWqMc=; b=YbrS1rTCXkBCIbh142yEwubaBQ2XkSJH/0xKf0lf6tUumEJSdjOkNie1Nx61lojoXo 7ghRJ4k9WRR4jECQ0Dr2ZPBq/WU7t6xV2yqW0HATlcT/2Yl8k5zRPz4Fct1kN9ZfM/ig wiY+zF/49Otr9jZ/sUBro5DEDVmoThVwMV6c4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=P/zvL47bzMX9zv6R9Iu19Vam9aHyZ/GbOGpqtXAWqMc=; b=TroEfw/1z0Ad8eVP8D/ZItQv7X84kLO/b8bDOLoG1nYIWDhWPfHyuOU4Mu4AX5LN+v 4t4h20UnFdOHTZl46G5T8/3d4NsWsIMqdBdof8WMGR5HoFEiL7UA7yLEJLr+hpOzRWQZ bDaXO2gBIn3QpTn4EFzTfeHKI58SOWP/KW3x89AP7uMVjpIfJoQEWc5JADsoV453h6E5 bue/Ojvf7q0HS3Iy+cbaS8typXlMfq0M6BhELuUdKnRaEyI6J+9+dPgBEfhZoPsSpdSm dOYhqhMoOkPS2rX1FJL9CxdOtHXe5CDP3q6MLoY6r+DaSIq3CW/ZTZRgaER2TVoaj1bW ZtIg== X-Gm-Message-State: AJIora9FPJtLy5UasXlA1gK7nq+44Hj/7irB9QPZkgmInnXQcWjU+prQ ekiJidJVWPhvbcNvlDk19UZYcg== X-Google-Smtp-Source: AGRyM1ufL5MDqjvHIo4wa13zoHeLqce3gwGBqjZkT/cfGeJacNorGFZd87ZD1rOQxbOuCm8UywPhnQ== X-Received: by 2002:a05:6870:c151:b0:fe:251b:804c with SMTP id g17-20020a056870c15100b000fe251b804cmr3187763oad.15.1655233152767; Tue, 14 Jun 2022 11:59:12 -0700 (PDT) Received: from [192.168.0.41] ([184.4.90.121]) by smtp.gmail.com with ESMTPSA id x64-20020acae043000000b0032ecb7370ffsm5017316oig.41.2022.06.14.11.59.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Jun 2022 11:59:10 -0700 (PDT) Message-ID: <9ed91f15-420c-3db6-8b3b-85438b02bf97@cloudflare.com> Date: Tue, 14 Jun 2022 13:59:08 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: [PATCH v3] cred: Propagate security_prepare_creds() error code Content-Language: en-US To: "Eric W. Biederman" Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, linux-cachefs@redhat.com, linux-cifs@vger.kernel.org, samba-technical@lists.samba.org, linux-mm@kvack.org, linux-nfs@vger.kernel.org, linux-unionfs@vger.kernel.org, linux-security-module@vger.kernel.org, netdev@vger.kernel.org, keyrings@vger.kernel.org, selinux@vger.kernel.org, serge@hallyn.com, amir73il@gmail.com, kernel-team@cloudflare.com, Jeff Moyer , Paul Moore References: <20220608150942.776446-1-fred@cloudflare.com> <87tu8oze94.fsf@email.froward.int.ebiederm.org> <87y1xzyhub.fsf@email.froward.int.ebiederm.org> <859cb593-9e96-5846-2191-6613677b07c5@cloudflare.com> <87o7yvxl4x.fsf@email.froward.int.ebiederm.org> From: Frederick Lawler In-Reply-To: <87o7yvxl4x.fsf@email.froward.int.ebiederm.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655233153; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=P/zvL47bzMX9zv6R9Iu19Vam9aHyZ/GbOGpqtXAWqMc=; b=Nk2qMgjUpGzxUmHfa86jEprJBqOA88Q/0qfauzYXm/WqmPE78SCyFRO1ad8hqrKR4v+wD1 9PWQe31SDi46mLbXwO2Ox59UN9J1SC+fhDaSP+y1d+s70H7VJHzzQFHyBRAYX1SIIJHVbs qBqaBrwauF4KG/Fi6IlqFVFiZLoTaoQ= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=cloudflare.com header.s=google header.b=YbrS1rTC; dmarc=pass (policy=reject) header.from=cloudflare.com; spf=none (imf12.hostedemail.com: domain of fred@cloudflare.com has no SPF policy when checking 209.85.160.49) smtp.mailfrom=fred@cloudflare.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655233153; a=rsa-sha256; cv=none; b=PMWF3eMhPI62Uv2qWxQJ+lE1f7wCGBJ+RDfl/ezbAFNB8oigBEbxn0Pdz6Bw1T6wHrwxdM ohW7B7CE8tpTJAsA7iTfesIgu9MIAZh//7JA7Alg/TVp/GIId6ZBCcihSG6Mjokze2NmB9 Zp7Z+zl1VCH54wwhPxwvbhykyKFcJdY= X-Rspamd-Queue-Id: 872B6400A7 X-Rspam-User: X-Stat-Signature: wyx9depca9zxpw7bicpk168k3zt78tow Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=cloudflare.com header.s=google header.b=YbrS1rTC; dmarc=pass (policy=reject) header.from=cloudflare.com; spf=none (imf12.hostedemail.com: domain of fred@cloudflare.com has no SPF policy when checking 209.85.160.49) smtp.mailfrom=fred@cloudflare.com X-Rspamd-Server: rspam04 X-HE-Tag: 1655233153-838749 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 6/14/22 11:30 AM, Eric W. Biederman wrote: > Frederick Lawler writes: > >> On 6/13/22 11:44 PM, Eric W. Biederman wrote: >>> Frederick Lawler writes: >>> >>>> Hi Eric, >>>> >>>> On 6/13/22 12:04 PM, Eric W. Biederman wrote: >>>>> Frederick Lawler writes: >>>>> >>>>>> While experimenting with the security_prepare_creds() LSM hook, we >>>>>> noticed that our EPERM error code was not propagated up the callstack. >>>>>> Instead ENOMEM is always returned. As a result, some tools may send a >>>>>> confusing error message to the user: >>>>>> >>>>>> $ unshare -rU >>>>>> unshare: unshare failed: Cannot allocate memory >>>>>> >>>>>> A user would think that the system didn't have enough memory, when >>>>>> instead the action was denied. >>>>>> >>>>>> This problem occurs because prepare_creds() and prepare_kernel_cred() >>>>>> return NULL when security_prepare_creds() returns an error code. Later, >>>>>> functions calling prepare_creds() and prepare_kernel_cred() return >>>>>> ENOMEM because they assume that a NULL meant there was no memory >>>>>> allocated. >>>>>> >>>>>> Fix this by propagating an error code from security_prepare_creds() up >>>>>> the callstack. >>>>> Why would it make sense for security_prepare_creds to return an error >>>>> code other than ENOMEM? >>>>> > That seems a bit of a violation of what that function is supposed to do >>>>> >>>> >>>> The API allows LSM authors to decide what error code is returned from the >>>> cred_prepare hook. security_task_alloc() is a similar hook, and has its return >>>> code propagated. >>> It is not an api. It is an implementation detail of the linux kernel. >>> It is a set of convenient functions that do a job. >>> The general rule is we don't support cases without an in-tree user. I >>> don't see an in-tree user. >>> >>>> I'm proposing we follow security_task_allocs() pattern, and add visibility for >>>> failure cases in prepare_creds(). >>> I am asking why we would want to. Especially as it is not an API, and I >>> don't see any good reason for anything but an -ENOMEM failure to be >>> supported. >>> >> We're writing a LSM BPF policy, and not a new LSM. Our policy aims to solve >> unprivileged unshare, similar to Debian's patch [1]. We're in a position such >> that we can't use that patch because we can't block _all_ of our applications >> from performing an unshare. We prefer a granular approach. LSM BPF seems like a >> good choice. > > I am quite puzzled why doesn't /proc/sys/user/max_user_namespaces work > for you? > We have the following requirements: 1. Allow list criteria 2. root user must be able to create namespaces whenever 3. Everything else not in 1 & 2 must be denied We use per task attributes to determine whether or not we allow/deny the current call to unshare(). /proc/sys/user/max_user_namespaces limits are a bit broad for this level of detail. >> Because LSM BPF exposes these hooks, we should probably treat them as an >> API. From that perspective, userspace expects unshare to return a EPERM >> when the call is denied permissions. > > The BPF code gets to be treated as a out of tree kernel module. > >>> Without an in-tree user that cares it is probably better to go the >>> opposite direction and remove the possibility of return anything but >>> memory allocation failure. That will make it clearer to implementors >>> that a general error code is not supported and this is not a location >>> to implement policy, this is only a hook to allocate state for the LSM. >>> >> >> That's a good point, and it's possible we're using the wrong hook for the >> policy. Do you know of other hooks we can look into? > > Not off the top of my head. > >>>>> I have probably missed a very interesting discussion where that was >>>>> mentioned but I don't see link to the discussion or anything explaining >>>>> why we want to do that in this change. >>>>> >>>> >>>> AFAIK, this is the start of the discussion. >>> You were on v3 and had an out of tree piece of code so I assumed someone >>> had at least thought about why you want to implement policy in a piece >>> of code whose only purpose is to allocate memory to store state. >>> >> >> No worries. >> >>> Eric >>> >>> >> >> Links: >> 1: >> https://sources.debian.org/patches/linux/3.16.56-1+deb8u1/debian/add-sysctl-to-disallow-unprivileged-CLONE_NEWUSER-by-default.patch/ > > Eric