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 X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49FF2C43461 for ; Fri, 16 Apr 2021 12:42:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2CFC0611AB for ; Fri, 16 Apr 2021 12:42:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235243AbhDPMnI (ORCPT ); Fri, 16 Apr 2021 08:43:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235414AbhDPMnI (ORCPT ); Fri, 16 Apr 2021 08:43:08 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 053A0C061760 for ; Fri, 16 Apr 2021 05:42:42 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id r22so20203539ljc.5 for ; Fri, 16 Apr 2021 05:42:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=xSvAjT7fJoqP/gXy5cezTHjGsp7EVSxfqA5TLEclTpE=; b=N2R8BSwyq6L8HhG5VF4U5Z8XYGGb6pNZoJAMJ/1ur+kAdwX70KKdhm9t/ER//BaaGC fC4yv1tQxMxfYFuoTo68rLF2UuyW3JekVhz9+xQTJG4RX1Wt0FDpW6dQvwTKDH8OM8Zz XAo0SRVp1WoY1+uTX8CCAhHkBip3hAiwW/z33RlDq4grtd4yD6iC5gCsYfE3/YttwM74 9ESseF9WStozPeMahssUKibfjLnzyh0IYD2MntIGJfmz/2Q41q3iwDSPRZEbSLBVmSor LIkZIbmEOSH4ZRpgTXu6gbMHD7g9CJ48nYg3M1i/vvv2u+k7s9/zlK1edCwq5oRehlTe 3GyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=xSvAjT7fJoqP/gXy5cezTHjGsp7EVSxfqA5TLEclTpE=; b=N2NF9wbB+vP2DxGPJgGC1lI69j5d6fpgx8cc5T5Py3N3U+saMqfD1C0Vt6nVLP4NKm SohX8iuJLoVy9OnIgIggqcKsDtpQeIsnRWBKJ4Wlii033KVKE18ePSTO0tJZEdwtwnKg Ma7fMNpBVKXPm/qX/z1z+SiHPslmclpWOfl3ShE7u6UMRzReMKIcwgorwNl9h3rkkrOn G8flZS8KKzeza2bvkthPhM+JrFLFAH++OFxzj6rdxOsZJuTCAzPGfbZMBs2Vgoe+YExj vB56aDSX2F9n7PAhSmHf6Br+gP54F8q2ojyGz7WS/prsSEUoPQ6iZSSq8KznjKn3sQ8W 3uqQ== X-Gm-Message-State: AOAM533TDmV+PLztNSW10zRAkzyfBb9Qiu2LYWzRreTIQ3o/1458xgdU OWPmkuLreLRY0BxBW5LGkqX2Ue7+HtdNlbHZdj+Omw== X-Google-Smtp-Source: ABdhPJxpPsOb5Vn7O+jFE45i/Oi8eyCRoGdm3iVzaaI0n2gDmIuJE5RiWb1Ekfzr6QRG9wCTQ7AnfakUvrfliDClOow= X-Received: by 2002:a2e:7607:: with SMTP id r7mr2559286ljc.226.1618576961144; Fri, 16 Apr 2021 05:42:41 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Sumit Garg Date: Fri, 16 Apr 2021 18:12:29 +0530 Message-ID: Subject: Re: [PATCH] KEYS: trusted: fix a couple error pointer dereferences To: Dan Carpenter Cc: James Bottomley , Jarkko Sakkinen , Mimi Zohar , David Howells , James Morris , "Serge E. Hallyn" , linux-integrity , "open list:ASYMMETRIC KEYS" , "open list:SECURITY SUBSYSTEM" , kernel-janitors@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: Hi Dan, On Wed, 14 Apr 2021 at 11:39, Dan Carpenter wrote: > > If registering "reg_shm_out" fails, then it is an error pointer and the > error handling will call tee_shm_free(reg_shm_out) which leads to an > error pointer dereference and an Oops. > > I've re-arranged it so we only free things that have been allocated > successfully. > > Fixes: 6dd95e650c8a ("KEYS: trusted: Introduce TEE based Trusted Keys") > Signed-off-by: Dan Carpenter > --- > security/keys/trusted-keys/trusted_tee.c | 24 ++++++++++-------------- > 1 file changed, 10 insertions(+), 14 deletions(-) > Thanks for fixing the error path. Although an alternative simple fix could be [1] but I liked your approach to get rid of redundant "if" checks. FWIW: Reviewed-by: Sumit Garg [1] diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c index 2ce66c199e1d..c4a54b0fa3eb 100644 --- a/security/keys/trusted-keys/trusted_tee.c +++ b/security/keys/trusted-keys/trusted_tee.c @@ -84,6 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) if (IS_ERR(reg_shm_out)) { dev_err(pvt_data.dev, "blob shm register failed\n"); ret = PTR_ERR(reg_shm_out); + reg_shm_out = NULL; goto out; } @@ -145,6 +146,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob) if (IS_ERR(reg_shm_out)) { dev_err(pvt_data.dev, "key shm register failed\n"); ret = PTR_ERR(reg_shm_out); + reg_shm_out = NULL; goto out; } -Sumit > diff --git a/security/keys/trusted-keys/trusted_tee.c b/security/keys/trusted-keys/trusted_tee.c > index 2ce66c199e1d..45f96f6ed673 100644 > --- a/security/keys/trusted-keys/trusted_tee.c > +++ b/security/keys/trusted-keys/trusted_tee.c > @@ -65,7 +65,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) > int ret; > struct tee_ioctl_invoke_arg inv_arg; > struct tee_param param[4]; > - struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL; > + struct tee_shm *reg_shm_in, *reg_shm_out; > > memset(&inv_arg, 0, sizeof(inv_arg)); > memset(¶m, 0, sizeof(param)); > @@ -84,7 +84,7 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) > if (IS_ERR(reg_shm_out)) { > dev_err(pvt_data.dev, "blob shm register failed\n"); > ret = PTR_ERR(reg_shm_out); > - goto out; > + goto free_shm_in; > } > > inv_arg.func = TA_CMD_SEAL; > @@ -109,11 +109,9 @@ static int trusted_tee_seal(struct trusted_key_payload *p, char *datablob) > p->blob_len = param[1].u.memref.size; > } > > -out: > - if (reg_shm_out) > - tee_shm_free(reg_shm_out); > - if (reg_shm_in) > - tee_shm_free(reg_shm_in); > + tee_shm_free(reg_shm_out); > +free_shm_in: > + tee_shm_free(reg_shm_in); > > return ret; > } > @@ -126,7 +124,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob) > int ret; > struct tee_ioctl_invoke_arg inv_arg; > struct tee_param param[4]; > - struct tee_shm *reg_shm_in = NULL, *reg_shm_out = NULL; > + struct tee_shm *reg_shm_in, *reg_shm_out; > > memset(&inv_arg, 0, sizeof(inv_arg)); > memset(¶m, 0, sizeof(param)); > @@ -145,7 +143,7 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob) > if (IS_ERR(reg_shm_out)) { > dev_err(pvt_data.dev, "key shm register failed\n"); > ret = PTR_ERR(reg_shm_out); > - goto out; > + goto free_shm_in; > } > > inv_arg.func = TA_CMD_UNSEAL; > @@ -170,11 +168,9 @@ static int trusted_tee_unseal(struct trusted_key_payload *p, char *datablob) > p->key_len = param[1].u.memref.size; > } > > -out: > - if (reg_shm_out) > - tee_shm_free(reg_shm_out); > - if (reg_shm_in) > - tee_shm_free(reg_shm_in); > + tee_shm_free(reg_shm_out); > +free_shm_in: > + tee_shm_free(reg_shm_in); > > return ret; > } > -- > 2.30.2 >