From patchwork Thu Dec 19 21:58:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Petr_=C5=A0tetiar?= X-Patchwork-Id: 1213728 X-Patchwork-Delegate: ynezz@true.cz Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=true.cz Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="Mi678JVk"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47f5RL2fPyz9sR1 for ; Fri, 20 Dec 2019 09:00:45 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Subject:MIME-Version:References: In-Reply-To:Message-Id:Date:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zjXWjrg0NozVfUHNZLScjpc/BcmsusYaSP3UDA4muiE=; b=Mi678JVk4KKClQ RbVjStWiCjUdHka/vkcOu3Ad87jGXUyaYnFq+G7cW3VRhpZiKS+x7Qpm65WwOiTIuJ9ctA7y8WB6Y NZ8IczpXB2tFGJ4p5Co1eG6fS+PIwhFYuPUic3EZxLVAqO099mvg3iFsEKxVbBqOBbor5nUv/zXWp rk9G98WMJI1KhaZWcT8W754XIJzEi03P9fgFHyIe66VATH1HsgVwNmJ8QJ/m8FDN/85nZkgMnZysH 6dcFhUXbLfuOR6ufQGCDMhz19taUloKy0H2g7F1w7TyyHyDG+IqMvniW3aSDMpzJOFJm/7380bOuM BE4VI/hIBu6Ucq+bUIGQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1ii3qd-0004rk-Cx; Thu, 19 Dec 2019 22:00:43 +0000 Received: from smtp-out.xnet.cz ([178.217.244.18]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1ii3oq-0000ta-Eo for openwrt-devel@lists.openwrt.org; Thu, 19 Dec 2019 21:58:55 +0000 Received: from meh.true.cz (meh.true.cz [108.61.167.218]) (Authenticated sender: petr@true.cz) by smtp-out.xnet.cz (Postfix) with ESMTPSA id 47C494B4C; Thu, 19 Dec 2019 22:58:47 +0100 (CET) Received: by meh.true.cz (OpenSMTPD) with ESMTP id dafc6f3d; Thu, 19 Dec 2019 22:58:35 +0100 (CET) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Date: Thu, 19 Dec 2019 22:58:25 +0100 Message-Id: <20191219215836.21773-10-ynezz@true.cz> In-Reply-To: <20191219215836.21773-1-ynezz@true.cz> References: <20191219215836.21773-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191219_135852_706390_0AF8929D X-CRM114-Status: UNSURE ( 9.47 ) X-CRM114-Notice: Please train this message. X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [178.217.244.18 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record Subject: [OpenWrt-Devel] [PATCH libubox 09/20] blob: introduce blob_parse_untrusted X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Petr_=C5=A0tetiar?= Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org blob_parse can be only used on trusted input as it has no possibility to check the length of the provided input buffer, which might lead to undefined behaviour and/or crashes when supplied with malformed, corrupted or otherwise specially crafted input. So this introduces blob_parse_untrusted variant which expects additional input buffer length argument and thus should be able to process also inputs from untrusted sources. Signed-off-by: Petr Štetiar --- blob.c | 24 ++++++++++++++++++++++++ blob.h | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/blob.c b/blob.c index ee93894b9e6f..dc908d9ea745 100644 --- a/blob.c +++ b/blob.c @@ -252,6 +252,30 @@ blob_parse_attr(struct blob_attr *attr, struct blob_attr **data, const struct bl return found; } +int +blob_parse_untrusted(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max) +{ + struct blob_attr *pos; + size_t len = 0; + int found = 0; + size_t rem; + + if (!attr || attr_len < sizeof(struct blob_attr)) + return 0; + + len = blob_raw_len(attr); + if (len != attr_len) + return 0; + + memset(data, 0, sizeof(struct blob_attr *) * max); + blob_for_each_attr_len(pos, attr, len, rem) { + found += blob_parse_attr(pos, rem, data, info, max); + } + + return found; +} + +/* use only on trusted input, otherwise consider blob_parse_untrusted */ int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max) { diff --git a/blob.h b/blob.h index d34652229b59..af033607e309 100644 --- a/blob.h +++ b/blob.h @@ -199,6 +199,7 @@ extern void blob_nest_end(struct blob_buf *buf, void *cookie); extern struct blob_attr *blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len); extern bool blob_check_type(const void *ptr, unsigned int len, int type); extern int blob_parse(struct blob_attr *attr, struct blob_attr **data, const struct blob_attr_info *info, int max); +extern int blob_parse_untrusted(struct blob_attr *attr, size_t attr_len, struct blob_attr **data, const struct blob_attr_info *info, int max); extern struct blob_attr *blob_memdup(struct blob_attr *attr); extern struct blob_attr *blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len); @@ -254,5 +255,11 @@ blob_put_u64(struct blob_buf *buf, int id, uint64_t val) (blob_pad_len(pos) >= sizeof(struct blob_attr)); \ rem -= blob_pad_len(pos), pos = blob_next(pos)) +#define blob_for_each_attr_len(pos, attr, attr_len, rem) \ + for (rem = attr ? blob_len(attr) : 0, \ + pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \ + rem >= sizeof(struct blob_attr) && rem < attr_len && (blob_pad_len(pos) <= rem) && \ + (blob_pad_len(pos) >= sizeof(struct blob_attr)); \ + rem -= blob_pad_len(pos), pos = blob_next(pos)) #endif