From patchwork Tue Jun 12 06:05:00 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 928111 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.b="SOXzuESe"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 414fcb2j4cz9s1R for ; Tue, 12 Jun 2018 16:10:07 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 15C41C21E39; Tue, 12 Jun 2018 06:08:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id D39DAC21E12; Tue, 12 Jun 2018 06:06:12 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 65168C21E50; Tue, 12 Jun 2018 06:05:41 +0000 (UTC) Received: from mail-it0-f68.google.com (mail-it0-f68.google.com [209.85.214.68]) by lists.denx.de (Postfix) with ESMTPS id 09616C21E12 for ; Tue, 12 Jun 2018 06:05:38 +0000 (UTC) Received: by mail-it0-f68.google.com with SMTP id m194-v6so13249486itg.2 for ; Mon, 11 Jun 2018 23:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=TaO68Y1PT4mt54s9xw7x4kWAwNVqB9Yy9lkSi639x4A=; b=SOXzuESeS1BXheEfYC9tmLN0MbuwIIls7ZUA1dCYholQ+lHvi/ApCP/Y2qmL8IkNZ+ ldNfRCUbBgnf3FKB3BDpKBRWnRY9szksM+snxFOe6nZDTTaluwn8F2ybOqD0YsR6qjzt n46ejfRX3/2sZqmE1jf7WzHQ7AQUDNnAmOFd7VJpJnJhw5HDI8389pkNZcLDv96mBFLr Mu1r0VXijRRlhq/gHmhvp5LDJyQgMwucKr1gurtQzEzIXsePbyX4mHLszHEo6dmieJQo gbjUOBs4R/z0FLNzTnX1OZcqPLlQtC+jAl3K0OW8yoIpJDnEgNMmKPXlIXqp1QY+ffXb I5rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=TaO68Y1PT4mt54s9xw7x4kWAwNVqB9Yy9lkSi639x4A=; b=MMisGMrVv+t94Uod4Xd2AYor9o9MFDJB8kWY6iX2DEqjSThFuMOZ1/bYejJ+1/Sl/G qRfn8mDcnGVwXWiCqvactcKkKkQJwuWb5/Ca3PNBB9eWV8UzwGilET0cQr14uL7ES8V2 tZouY7I6HgTD0j9sfRnbM7d4pNPbEJp/biJSEeoll6njfT49/il0VtBlnoeW0Bq5p4vt tzkeLxKi0fnSfnCY0STz5JznwE+nCllcu2k4uD0W8n1QUYJG5Rx6Qp+Qo6T0f13MGxy0 S5kYEiVy8idcp2biA3u91pXgy/Il2ywB504kt2QP5DtJXLswEh/qr9RcnQvHSxddANP2 h0+w== X-Gm-Message-State: APt69E0iVsnggtpMcRnuE42zojvXpJuY3z23idALnw5lhgk7W9faR+SR PTN/Kvu5jeD04cMFqAPkTPVyhQ== X-Google-Smtp-Source: ADUXVKJVurPX0haxirSSSkQDTEi1jqHtxUC8QMjxUSpPUFVJ5SMO68/3v8udLtA7ur4OScQv4bCGtA== X-Received: by 2002:a24:b715:: with SMTP id h21-v6mr1969122itf.32.1528783536462; Mon, 11 Jun 2018 23:05:36 -0700 (PDT) Received: from kiwi.bld.corp.google.com ([2620:15c:183:0:8223:87c:a681:66aa]) by smtp.gmail.com with ESMTPSA id t123-v6sm161050itb.14.2018.06.11.23.05.35 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 11 Jun 2018 23:05:35 -0700 (PDT) Received: by kiwi.bld.corp.google.com (Postfix, from userid 121222) id 67D48140129; Tue, 12 Jun 2018 00:05:35 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Tue, 12 Jun 2018 00:05:00 -0600 Message-Id: <20180612060502.196817-7-sjg@chromium.org> X-Mailer: git-send-email 2.18.0.rc1.244.gcf134e6275-goog In-Reply-To: <20180612060502.196817-1-sjg@chromium.org> References: <20180612060502.196817-1-sjg@chromium.org> Cc: Heinrich Schuchardt , Tom Rini Subject: [U-Boot] [PATCH v2 6/8] rsa: Fix missing memory leak on error in fdt_add_bignum() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Thsi function can fail without freeing all its memory. Fix it. Reported-by: Coverity (CID: 131217) Signed-off-by: Simon Glass --- Changes in v2: - Add a comment defending the technical memory leaks that remain lib/rsa/rsa-sign.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/rsa/rsa-sign.c b/lib/rsa/rsa-sign.c index d2788bf79a..cfe09cc94c 100644 --- a/lib/rsa/rsa-sign.c +++ b/lib/rsa/rsa-sign.c @@ -635,6 +635,15 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, big2 = BN_new(); big32 = BN_new(); big2_32 = BN_new(); + + /* + * Note: This code assumes that all of the above succeed, or all fail. + * In practice memory allocations generally do not fail (unless the + * process is killed), so it does not seem worth handling each of these + * as a separate case. Technicaly this could leak memory on failure, + * but a) it won't happen in practice, and b) it doesn't matter as we + * will immediately exit with a failure code. + */ if (!tmp || !big2 || !big32 || !big2_32) { fprintf(stderr, "Out of memory (bignum)\n"); return -ENOMEM; @@ -667,15 +676,13 @@ static int fdt_add_bignum(void *blob, int noffset, const char *prop_name, * might fail several times */ ret = fdt_setprop(blob, noffset, prop_name, buf, size); - if (ret) - return -FDT_ERR_NOSPACE; free(buf); BN_free(tmp); BN_free(big2); BN_free(big32); BN_free(big2_32); - return ret; + return ret ? -FDT_ERR_NOSPACE : 0; } int rsa_add_verify_data(struct image_sign_info *info, void *keydest)