Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1k0pSy-0001iE-CS for mharc-grub-devel@gnu.org; Wed, 29 Jul 2020 13:02:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40872) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0pSx-0001gN-B5 for grub-devel@gnu.org; Wed, 29 Jul 2020 13:02:07 -0400 Received: from userp2120.oracle.com ([156.151.31.85]:42782) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k0pSv-0000jc-9I for grub-devel@gnu.org; Wed, 29 Jul 2020 13:02:06 -0400 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 06TH1nh2037108; Wed, 29 Jul 2020 17:01:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references; s=corp-2020-01-29; bh=l/V6cS13nWD1Zwua+9HCb0x8Cf4CRbI4rTUBm1JqB44=; b=GYcpiQ1D3J2US0W+C2OvcEHjdsARZtUwU0EKxg0L9jiLe6ArM3cEgA41ooj8HZmVwjmz UrcCKjfNrMWEtJw/5YvjkOmeme2Vzyn8F+I6KrwGZuujjMbHgNvp0Q+if1RQA36fOLXS FdEqvfFI0vkzz/79MFC6kgN5RUABslO3IxOLpAzujl7bqUIKhkGxfRbPOiTwebT7NsbE grtvMbslgbrfTN6lE/u+yeHFYCxLlrQhk6wqpe63PRm6LaqqIjf4o0WRuAzU7mFnmNsN cRKcEu/QwrDpdGDj5uEQzXW0CFQB+BaidkDXHYVQO5ETCbqwQFyBniO8s9FQmKwSCZQR fw== Received: from userp3030.oracle.com (userp3030.oracle.com [156.151.31.80]) by userp2120.oracle.com with ESMTP id 32hu1jpup4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 29 Jul 2020 17:01:57 +0000 Received: from pps.filterd (userp3030.oracle.com [127.0.0.1]) by userp3030.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 06TGvPWH035418; Wed, 29 Jul 2020 17:01:57 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userp3030.oracle.com with ESMTP id 32hu5v5gx4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 29 Jul 2020 17:01:56 +0000 Received: from abhmp0014.oracle.com (abhmp0014.oracle.com [141.146.116.20]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id 06TH1tTw021772; Wed, 29 Jul 2020 17:01:55 GMT Received: from tomti.i.net-space.pl (/10.175.200.191) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 29 Jul 2020 10:01:54 -0700 From: Daniel Kiper To: grub-devel@gnu.org Cc: 93sam@debian.org, alexander.burmashev@oracle.com, amakhalov@vmware.com, chris.coulson@canonical.com, cjwatson@debian.org, cperry@redhat.com, darren.kenny@oracle.com, darren.moffat@oracle.com, dave.miner@oracle.com, degranit@microsoft.com, eric.snowberg@oracle.com, ilya.okomin@oracle.com, jan.setjeeilers@oracle.com, jerecox@microsoft.com, jesse@eclypsium.com, john.haxby@oracle.com, kanth.ghatraju@oracle.com, konrad.wilk@oracle.com, mbenatto@redhat.com, mickey@eclypsium.com, msrc57813grub@microsoft.com, phcoder@gmail.com, pjones@redhat.com, sajacobu@microsoft.com, todd.vierling@oracle.com, xnox@ubuntu.com Subject: [SECURITY PATCH 10/28] json: Avoid a double-free when parsing fails. Date: Wed, 29 Jul 2020 19:00:23 +0200 Message-Id: <20200729170041.14082-11-daniel.kiper@oracle.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20200729170041.14082-1-daniel.kiper@oracle.com> References: <20200729170041.14082-1-daniel.kiper@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9697 signatures=668679 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 mlxlogscore=999 mlxscore=0 suspectscore=3 bulkscore=0 malwarescore=0 spamscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007290115 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9697 signatures=668679 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 clxscore=1015 mlxlogscore=999 malwarescore=0 impostorscore=0 priorityscore=1501 spamscore=0 phishscore=0 suspectscore=3 bulkscore=0 mlxscore=0 lowpriorityscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2006250000 definitions=main-2007290116 Received-SPF: pass client-ip=156.151.31.85; envelope-from=daniel.kiper@oracle.com; helo=userp2120.oracle.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/07/29 13:01:26 X-ACL-Warn: Detected OS = Linux 3.1-3.10 [fuzzy] X-Spam_score_int: -53 X-Spam_score: -5.4 X-Spam_bar: ----- X-Spam_report: (-5.4 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jul 2020 17:02:07 -0000 From: Chris Coulson When grub_json_parse() succeeds, it returns the root object which contains a pointer to the provided JSON string. Callers are responsible for ensuring that this string outlives the root object and for freeing its memory when it's no longer needed. If grub_json_parse() fails to parse the provided JSON string, it frees the string before returning an error. This results in a double free in luks2_recover_key(), which also frees the same string after grub_json_parse() returns an error. This changes grub_json_parse() to never free the JSON string passed to it, and updates the documentation for it to make it clear that callers are responsible for ensuring that the string outlives the root JSON object. Fixes: CID 292465 Signed-off-by: Chris Coulson Reviewed-by: Daniel Kiper --- grub-core/lib/json/json.c | 9 +++------ grub-core/lib/json/json.h | 5 ++++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/grub-core/lib/json/json.c b/grub-core/lib/json/json.c index 913b8f404..1c20c75ea 100644 --- a/grub-core/lib/json/json.c +++ b/grub-core/lib/json/json.c @@ -71,12 +71,9 @@ grub_json_parse (grub_json_t **out, char *string, grub_size_t string_len) *out = json; err: - if (ret && json) - { - grub_free (json->string); - grub_free (json->tokens); - grub_free (json); - } + if (ret) + grub_json_free (json); + return ret; } diff --git a/grub-core/lib/json/json.h b/grub-core/lib/json/json.h index d9f99454d..01614f6df 100644 --- a/grub-core/lib/json/json.h +++ b/grub-core/lib/json/json.h @@ -50,7 +50,10 @@ typedef struct grub_json grub_json_t; * Parse a JSON-encoded string. Note that the string passed to * this function will get modified on subsequent calls to * grub_json_get*(). Returns the root object of the parsed JSON - * object, which needs to be free'd via grub_json_free(). + * object, which needs to be free'd via grub_json_free(). Callers + * must ensure that the string outlives the returned root object, + * and that child objects must not be used after the root object + * has been free'd. */ extern grub_err_t EXPORT_FUNC(grub_json_parse) (grub_json_t **out, char *string, -- 2.11.0