Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp641684iog; Mon, 13 Jun 2022 09:43:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1urGswJIhVDVC6N3AxpPzvzc8iXvihpUM3yW6us25nANurEUstAVz1oUgcQhb9S3sH9h2As X-Received: by 2002:a17:902:6bc7:b0:168:e0b6:c740 with SMTP id m7-20020a1709026bc700b00168e0b6c740mr14399plt.156.1655138590205; Mon, 13 Jun 2022 09:43:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655138590; cv=none; d=google.com; s=arc-20160816; b=qzOIRRxuOk0lcNkdQszybxALPQmpSDeo0mvc1EFZQMT7No6lpBtjLmmESF32A2e+V+ PCI1CXX6hdh2UFMEXW9tUB2hSSHXiDnUfJ1B17XZ+S2i+DHtJS52jeE26ud4Ma6OOiJx CDkXyo96PLi1aw3/qBWwrdm4Wg7YA5Ezba6u8GRkoqgQwFsL+RyvtZvjkXQut6yLN42V 2Ww29Cv4jlpd+p1n+mkZxmaMj8gqk+j6rvyyF4M0Eo/S4p2dyBFCtGY8YR4hrmVlqq9k S5nMbLRy6FQvlNJg+mQbarpL92XQM2myC/1y9FBWOrXEUe90aVWCW4x6f9jkTPp474T3 tH/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=F8034aqgQYOIDnTJYbKnqdxe/P3NxT2RcNrYib0/ruQ=; b=U1arXC+5eB1yGL8kRZ3hiC+2dQEN4+XMkH1WwXzgDHutjsB+mrxhWr5LyarX8UyB4/ MHSBIyvhBcPfApSKvsTtVa36q3x3NyQjKRRIpTjoOpB9NF34pk4+iszXIOnNVAg7Heup u7a9lhrTYI6l/MhF2de2WbAjs9PBRwh+w+OioBKkDaSdeVB4oVYvqYhsCVGKkjpmIsrd 9d8CYkl46zLAEC4xI1V/p3kJfpFxj7+naU4L252DG1tpd7EAY9POvycdDcTvmEmQEisn sGz7F5tHoaliSlfYr+UnP6FM0n+jaYe7jUAsy827UX3/RzYH9I2oC1gZY4PnG2qZXbiD 4TBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=R4MMR5hy; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nl13-20020a17090b384d00b001e82e1ec1adsi16095194pjb.3.2022.06.13.09.42.56; Mon, 13 Jun 2022 09:43:10 -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=@cloudflare.com header.s=google header.b=R4MMR5hy; 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=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242563AbiFMP4f (ORCPT + 99 others); Mon, 13 Jun 2022 11:56:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242820AbiFMP4I (ORCPT ); Mon, 13 Jun 2022 11:56:08 -0400 Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D5DD019321D for ; Mon, 13 Jun 2022 06:46:52 -0700 (PDT) Received: by mail-oi1-x22c.google.com with SMTP id k24so7807265oij.2 for ; Mon, 13 Jun 2022 06:46:52 -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=F8034aqgQYOIDnTJYbKnqdxe/P3NxT2RcNrYib0/ruQ=; b=R4MMR5hyemRuM3l2mh4JZIS+Ysd67mGMs9w57QzxuAYNivT2oR2uA0ccLItOXx/Nff HI6/bkCTWcHzuf8uaxK0aZVKIRxAzJmo3pQ4NckjHUXisjQlQKqlTIlGGFMleFZ26TS5 XrbPJQzMEXp3LBINHndQ9fwG3XW2+NnK70OZQ= 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=F8034aqgQYOIDnTJYbKnqdxe/P3NxT2RcNrYib0/ruQ=; b=QsFOZX3A0Qk7GG+94uEhGqQ10zr0fsyK3yQcUtdV8GLJ36ucpMM01GeEnkV9EXJUe+ eX4ds9u2KQVjwTP9vLtYhJ7if8HkSNmSZL9wi7jG0BrZX6cN3IPm14+wI6dk37mj/EGw IEtOgnv/ydxY7Ch0U47glJ1IYuFuG4wLZNzttAySsEe7FGbr2GoOlNCqAevcAKEfP+ti lsuqyHURKkG1QUR7vle4fcimjelajxX3XUh9ZSZbpP+FAQM45/Zv9/YkxxpSNQXGdW1d PzXtNu/W62pexEkF/MTNJe6STeJ0aBFi2SxYi9MOCPgWvfw9M62Ik7gcwtg4qxAF498L 8aGw== X-Gm-Message-State: AOAM531bIi/vMie7sIxm8ECZBym3eDm5Ik4LDsWg3WG1HyFgeqR++28G X/+jD9TYWwo+IkUsJfvm1ljmPw== X-Received: by 2002:aca:b744:0:b0:32f:4c19:cec1 with SMTP id h65-20020acab744000000b0032f4c19cec1mr1696209oif.43.1655128012170; Mon, 13 Jun 2022 06:46:52 -0700 (PDT) Received: from [192.168.0.41] ([184.4.90.121]) by smtp.gmail.com with ESMTPSA id o20-20020a4ad494000000b0035eb4e5a6b5sm3699171oos.11.2022.06.13.06.46.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 13 Jun 2022 06:46:51 -0700 (PDT) Message-ID: Date: Mon, 13 Jun 2022 08:46:49 -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 Biggers 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> From: Frederick Lawler In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, 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 Hi Eric, On 6/9/22 6:18 PM, Eric Biggers wrote: > On Wed, Jun 08, 2022 at 10:09:42AM -0500, Frederick Lawler wrote: >> diff --git a/fs/aio.c b/fs/aio.c >> index 3c249b938632..5abbe88c3ca7 100644 >> --- a/fs/aio.c >> +++ b/fs/aio.c >> @@ -1620,6 +1620,8 @@ static void aio_fsync_work(struct work_struct *work) >> static int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb, >> bool datasync) >> { >> + int err; >> + >> if (unlikely(iocb->aio_buf || iocb->aio_offset || iocb->aio_nbytes || >> iocb->aio_rw_flags)) >> return -EINVAL; >> @@ -1628,8 +1630,11 @@ static int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb, >> return -EINVAL; >> >> req->creds = prepare_creds(); >> - if (!req->creds) >> - return -ENOMEM; >> + if (IS_ERR(req->creds)) { >> + err = PTR_ERR(req->creds); >> + req->creds = NULL; >> + return err; >> + } > > This part is a little ugly. How about doing: > > creds = prepare_creds(); > if (IS_ERR(creds)) > return PTR_ERR(creds); > req->creds = creds; > I can do that, and same for below. >> diff --git a/fs/exec.c b/fs/exec.c >> index 0989fb8472a1..02624783e40e 100644 >> --- a/fs/exec.c >> +++ b/fs/exec.c >> @@ -1468,15 +1468,19 @@ EXPORT_SYMBOL(finalize_exec); >> */ >> static int prepare_bprm_creds(struct linux_binprm *bprm) >> { >> + int err = -ERESTARTNOINTR; >> if (mutex_lock_interruptible(¤t->signal->cred_guard_mutex)) >> - return -ERESTARTNOINTR; >> + return err; >> >> bprm->cred = prepare_exec_creds(); >> - if (likely(bprm->cred)) >> - return 0; >> + if (IS_ERR(bprm->cred)) { >> + err = PTR_ERR(bprm->cred); >> + bprm->cred = NULL; >> + mutex_unlock(¤t->signal->cred_guard_mutex); >> + return err; >> + } >> >> - mutex_unlock(¤t->signal->cred_guard_mutex); >> - return -ENOMEM; >> + return 0; >> } > > Similarly: > > static int prepare_bprm_creds(struct linux_binprm *bprm) > { > struct cred *cred; > > if (mutex_lock_interruptible(¤t->signal->cred_guard_mutex)) > return -ERESTARTNOINTR; > > cred = prepare_exec_creds(); > if (IS_ERR(cred)) { > mutex_unlock(¤t->signal->cred_guard_mutex); > return PTR_ERR(cred); > } > bprm->cred = cred; > return 0; > } > >> diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c >> index eec72ca962e2..6cf75aa83b6c 100644 >> --- a/kernel/nsproxy.c >> +++ b/kernel/nsproxy.c >> @@ -311,6 +311,7 @@ static void put_nsset(struct nsset *nsset) >> >> static int prepare_nsset(unsigned flags, struct nsset *nsset) >> { >> + int err = -ENOMEM; >> struct task_struct *me = current; >> >> nsset->nsproxy = create_new_namespaces(0, me, current_user_ns(), me->fs); >> @@ -324,6 +325,12 @@ static int prepare_nsset(unsigned flags, struct nsset *nsset) >> if (!nsset->cred) >> goto out; >> >> + if (IS_ERR(nsset->cred)) { >> + err = PTR_ERR(nsset->cred); >> + nsset->cred = NULL; >> + goto out; >> + } > > Why is the NULL check above being kept? > In the branch prior: if (flags & CLONE_NEWUSER) { nsset->cred = prepare_creds(); else nsset->cred = current_cred(); I don't see cases where others are checking for null after current_cred(), therefore I can remove that check. > Also, drivers/crypto/ccp/sev-dev.c needs to be updated. > Nice catch! I clearly missed addition after the merge window. > - Eric