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 C86D9C433EF for ; Tue, 14 Jun 2022 16:06:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 33EDB6B0098; Tue, 14 Jun 2022 12:06:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ED306B0099; Tue, 14 Jun 2022 12:06:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1661B6B009A; Tue, 14 Jun 2022 12:06:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 07DA16B0099 for ; Tue, 14 Jun 2022 12:06:29 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id CEFF333F87 for ; Tue, 14 Jun 2022 16:06:28 +0000 (UTC) X-FDA: 79577318856.09.D8A820C Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) by imf24.hostedemail.com (Postfix) with ESMTP id 0A2C8180095 for ; Tue, 14 Jun 2022 16:06:27 +0000 (UTC) Received: by mail-oi1-f174.google.com with SMTP id u9so6854510oiv.12 for ; Tue, 14 Jun 2022 09:06:27 -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=X4xRWyF9vr8qnasqfhsd+VAGIzL+N4al4cfXIWCdc0g=; b=NLNTK/3yOlui/8PDUrYDNIdgpprrS90aWM+WjqJ+L2bMU+1Ozbmg2rD4fkMgfuWnpm aMGJEYfd5nogbWyMEV3d9AdrsFTbd/ai3JvhNvWmxQgkmSCNpEsOoNznE37pK/knJYQu 8Aw2b9wKcBroceGAhUyVkpxk3cOL+KGNAnLUM= 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=X4xRWyF9vr8qnasqfhsd+VAGIzL+N4al4cfXIWCdc0g=; b=e29e1+vG4eJtk5slKvlw/2b4y72YHozlsAT2keZlY9dRVwQYPl2KD7C3tyQd43+hM/ zd1pe9/Em8i4krIdCMBcVp80csVzOUuutfKynvfALNEflwS7xczf7W7/Mqnk+5YfUXx8 c7U/dlGotzl0CgIogDVH3LemD3+3xcmul2uo4h8qski7b0G9inexcT4a1wD8Oy73bt65 0amP1w7Lvh6YxXlXQO9YDcnHibpkrBYbFPyUeg47zLYSDeQ5OUIdmfz6u7XksQPUCxsN jb3rQCTstpQq2v6banDLQcLAhL+3pUwBOaqn6spAuJjVFkVnI40PpeUhHczVdXyJxLtd LeRg== X-Gm-Message-State: AOAM530KMJy3WJ7/vpxwSkyr5Yu2B53GYFmLXrIG1/m0Xkp1wzKaNIfa EZJ1NrC3864QUlqa/sWTigrwDA== X-Google-Smtp-Source: ABdhPJz9B/kSdK+3GB7ULQc4BQgTDB3yrng0XI1+iE7DuZFwNplTTKpOWFeQG4wG0wSczvdmVlxS5A== X-Received: by 2002:aca:3945:0:b0:32b:3a61:35d6 with SMTP id g66-20020aca3945000000b0032b3a6135d6mr2484880oia.293.1655222787108; Tue, 14 Jun 2022 09:06:27 -0700 (PDT) Received: from [192.168.0.41] ([184.4.90.121]) by smtp.gmail.com with ESMTPSA id d1-20020a0568301b6100b0060bec21ffcdsm4939272ote.22.2022.06.14.09.06.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Jun 2022 09:06:26 -0700 (PDT) Message-ID: <859cb593-9e96-5846-2191-6613677b07c5@cloudflare.com> Date: Tue, 14 Jun 2022 11:06:24 -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> From: Frederick Lawler In-Reply-To: <87y1xzyhub.fsf@email.froward.int.ebiederm.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1655222788; a=rsa-sha256; cv=none; b=ko0aI4Gf+OzeuwwwvIO814o7JonU1mD03ILHsuiTXzHelCa9uxvQBBSe0itBxT0a7H4xT+ dbxUyLS48iWoNfdgcpkOvq9nSgTX3cU2KKeqxfL66cVd2tem9F66GUEzoNxHjEQToStajI RRgi/xi8gMiqhgmuA6nNBgYAa/v1ysc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=cloudflare.com header.s=google header.b="NLNTK/3y"; dmarc=pass (policy=reject) header.from=cloudflare.com; spf=none (imf24.hostedemail.com: domain of fred@cloudflare.com has no SPF policy when checking 209.85.167.174) smtp.mailfrom=fred@cloudflare.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1655222788; 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=X4xRWyF9vr8qnasqfhsd+VAGIzL+N4al4cfXIWCdc0g=; b=4Qwux4UByFDpXn6OfHBP/M87FSwSd68uinEwXwN0G4qfb8U+vhZMNIA6gOzAJYbMafx9HG iCsEeBAXyrJn7runsRe8d+TMp1L67J9C3zEhWoln5eL0FfK8ubVwAOAnwYWHquNFj2GW4e rALq8h1UtKtdJUf9tQbuksC+ekBIMaw= X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0A2C8180095 X-Stat-Signature: xss4k3s3w95nfg1xwhoynx84c63mm7ct Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=cloudflare.com header.s=google header.b="NLNTK/3y"; dmarc=pass (policy=reject) header.from=cloudflare.com; spf=none (imf24.hostedemail.com: domain of fred@cloudflare.com has no SPF policy when checking 209.85.167.174) smtp.mailfrom=fred@cloudflare.com X-Rspam-User: X-HE-Tag: 1655222787-564943 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/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. 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. > 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? >>> 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/