From patchwork Wed Nov 28 01:35:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Schramm X-Patchwork-Id: 1004141 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) 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=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="KuD5LhcB"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="kagr2LCV"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 434NWm5WXjz9s3Z for ; Wed, 28 Nov 2018 12:35:31 +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=ebMqwCaY2ApQ9vf88bYNwVd3oNRncD02BbyhDJ4O/Wk=; b=KuD5LhcBTU32PN 6sbsd5pu0vx6gCyUiLAFB1Z8rmv/6CxYIW3iZeMB4K3AVUwDsuBy0hyNdfzB0maf/HtNtD0sgUTKI GB8mbKxdZPQoZysXnIeSM4OQ8eE4ewmkEV0Uk5EhAHeY1cnhkr+J/0Jw1B8EKNfIjGhvLjHtT8I1b AkRVlQ5J4kug/G62afb4vZBn3UPRTWkwxKhWnHEjWrEN27oz0QMGEu7NQg3kjieWWoESBjcocduhb Rr3S07EUyp0Z9lz9n4+MJswuN4P0i+MBYQ5mpDXtFGME9lhmGgawOmNFks6FibKn8w4EVivlX1xbL 8mD43L66oUU5TIjfI9HQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRol9-0003pG-DG; Wed, 28 Nov 2018 01:35:23 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRoky-0002Rs-C9 for openwrt-devel@lists.openwrt.org; Wed, 28 Nov 2018 01:35:13 +0000 Received: by mail-wr1-x442.google.com with SMTP id l9so24608833wrt.13 for ; Tue, 27 Nov 2018 17:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JePgHmJ8JL3Q8eMK/EU4Ea9q4lBbJOJO4VLDsJIZH4U=; b=kagr2LCV8ndLj6AY1ySs7BPhvrLmSQbgxmA5DRIwSTNdfTZ91m4duWxnX8XxxaznvU B+PnHO0W+qlS8mQNo4dN8rz5kEVsangh4JfnWn+u7fgWtcF83xP8SIQan1I+OVOee/z4 e4DEu4tbkJshu6T0jwnvQcxAXc3AQJZ0bHMO52j1PfOR1cZjNdvUVOh0g7ugOejmvnAf NlGkevZbgY4lQMZ4jHTiKFEBum5P/4codhLZWK1Wda4hPxB8Z/cdH9tVUu2SGbC6Ngnc 3FDu0KVQe2w8kmpH1JFnqxSnLrY8CHsGgCFr9RO2LYzVKTJpUevP4EiNOlcrqOrYepNR ogCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JePgHmJ8JL3Q8eMK/EU4Ea9q4lBbJOJO4VLDsJIZH4U=; b=DhENWbZFh9Qa3W88xm4PyIpQQ5dmENB3lhw0Ly4oP70hMCcc4vgnW7w3zoW4JeWC6L fwBkVmxUJGUN3dp6CJjLDvshl7G3aUJwLIUF3aQHSDWdt1rGhz7xnzj/tgXKif7ZKv8k K1R6QFQxcE6AziZi06whFxvI8J4i+vNuGpzvIHyiZ8XlUYxVuRLv3ZKREuCjAV1evTbN my9D09chnqu2lrEGPWAJs2V6LziHlLT2s0i3GIgVwqDFd7zWGyZ+eI9QnCL/IfVvgily BNbnddxFncRIs8qdPf0fC+MSLQW+wQm2rnm51fLFARmB8hdy7zraVM/c8a6wpnKMkWpw 85WA== X-Gm-Message-State: AA+aEWZLNWRY0sjhdNxGqEGcRLvTeRbfyqe8QHOs5Twz6Sa8I7iy3mMb JTJWBRHgCADNzHvbwDHTOCHkDMnF X-Google-Smtp-Source: AFSGD/UMrc+kBj28y1sqhplDGMyHPvMiCwGOKnvlCJ3lz3x67R5BX7XgpI1DxVzf69lnFCC2x+MLXg== X-Received: by 2002:a5d:488f:: with SMTP id g15mr8546286wrq.15.1543368899581; Tue, 27 Nov 2018 17:34:59 -0800 (PST) Received: from sunsetshimmer.lan ([2a02:8106:19:4900:3ab5:b1b6:c8c2:b39f]) by smtp.gmail.com with ESMTPSA id 80sm2031521wmv.6.2018.11.27.17.34.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 17:34:58 -0800 (PST) From: Tobias Schramm To: openwrt-devel@lists.openwrt.org Date: Wed, 28 Nov 2018 02:35:21 +0100 Message-Id: <20181128013523.5793-2-tobleminer@gmail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181128013523.5793-1-tobleminer@gmail.com> References: <20181128013523.5793-1-tobleminer@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181127_173512_419163_195D328A X-CRM114-Status: UNSURE ( 9.70 ) X-CRM114-Notice: Please train this message. X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:442 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (tobleminer[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH v3 1/3] Ensure blob_attr length check does not perform out of bounds reads X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tobias Schramm , nbd@nbd.name Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Before there might have been as little as one single byte left which would result in 3 bytes of blob_attr->id_len being out of bounds Signed-off-by: Tobias Schramm Acked-by: Yousong Zhou --- blob.h | 4 ++-- blobmsg.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/blob.h b/blob.h index a092f5d..2304f8c 100644 --- a/blob.h +++ b/blob.h @@ -242,7 +242,7 @@ blob_put_u64(struct blob_buf *buf, int id, uint64_t val) #define __blob_for_each_attr(pos, attr, rem) \ for (pos = (struct blob_attr *) attr; \ - rem > 0 && (blob_pad_len(pos) <= rem) && \ + rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \ (blob_pad_len(pos) >= sizeof(struct blob_attr)); \ rem -= blob_pad_len(pos), pos = blob_next(pos)) @@ -250,7 +250,7 @@ blob_put_u64(struct blob_buf *buf, int id, uint64_t val) #define blob_for_each_attr(pos, attr, rem) \ for (rem = attr ? blob_len(attr) : 0, \ pos = (struct blob_attr *) (attr ? blob_data(attr) : NULL); \ - rem > 0 && (blob_pad_len(pos) <= rem) && \ + rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \ (blob_pad_len(pos) >= sizeof(struct blob_attr)); \ rem -= blob_pad_len(pos), pos = blob_next(pos)) diff --git a/blobmsg.h b/blobmsg.h index b06ef59..c75f1d9 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -266,7 +266,7 @@ int blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, . #define blobmsg_for_each_attr(pos, attr, rem) \ for (rem = attr ? blobmsg_data_len(attr) : 0, \ pos = (struct blob_attr *) (attr ? blobmsg_data(attr) : NULL); \ - rem > 0 && (blob_pad_len(pos) <= rem) && \ + rem >= sizeof(struct blob_attr) && (blob_pad_len(pos) <= rem) && \ (blob_pad_len(pos) >= sizeof(struct blob_attr)); \ rem -= blob_pad_len(pos), pos = blob_next(pos)) From patchwork Wed Nov 28 01:35:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Schramm X-Patchwork-Id: 1004143 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) 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=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="nNv8Z3np"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="bVRn+Cv/"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 434NWw4Vzpz9s3Z for ; Wed, 28 Nov 2018 12:35:40 +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=kQvbgqGvuaRxsf9KerJZfFpmD6gkf7mv4HVsc94yMtg=; b=nNv8Z3npzaNaNa tZwoAMs8W3xy4WYEqT2+i1iAX5DYqmCKRCXJdIYy/i0Zqowf0Z7VRlpupUpni52lgJlXckoy/O7f3 cabcCiQ66gVE+SQbtXUef4RSzupPbJ0GWk+kGEjNsvU16fa4EdnB+P6yQPYx1IV3eXrZkDsXQU272 HVrXXliG7/LaUuqbsjVYrqnMdcP+hAG8ugPnr5nSIZjmovRUsChDUY7mB+W8VW8PmDkHVyP1v87VU TdniWgIRonQDLgtawEbz+q8uUTen7ySHkSw0zFgx7xo0bzqnWxPeFcKfJqXg/CowzpT8Xjp7JMX5F kZzNNB7889ZjmUhbSogw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRolL-000438-7u; Wed, 28 Nov 2018 01:35:35 +0000 Received: from mail-wm1-x343.google.com ([2a00:1450:4864:20::343]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRokz-0002SE-9M for openwrt-devel@lists.openwrt.org; Wed, 28 Nov 2018 01:35:14 +0000 Received: by mail-wm1-x343.google.com with SMTP id s14so646346wmh.1 for ; Tue, 27 Nov 2018 17:35:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=BjOtFQ5Ur5Bax9a6apNrEFtSMUri70Mcvf1sT7LGCbc=; b=bVRn+Cv/L4bhCOM4OzYmPSKIbG15vU/d7QcN0K8hspqY5hBtxC/hBlZz1cu2yU7j2g he7Cm5lHEDEtWz5cwg2N7CLvSHUCA+wZZoKqICCM3xSKmxkLf1G/t7vZFjMcCW5SRU44 ffHaqO4uS1ThylUDJ4cNco4ffcU/WjF3DtLIhMG7Tkl9sQyABs86wjciIeMLjhc9VlJ1 IKCRxhIBCHXYpoiz6D8TYZ4oR6tkNG6SKDNhARnI3xgF7dbDmWHww4rsRDI0FnmHNp10 2233fnM4m0TmQ5xOfrCCv56qxkiVYCeUaYsXI28mS1p4D0Bf50b8Y9nPyBOU84m3PK0f VXUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=BjOtFQ5Ur5Bax9a6apNrEFtSMUri70Mcvf1sT7LGCbc=; b=X5Err43P9aEZwMqFp8HRstjFMHDCspNd7MpB4FfUZS7SJpqezCVS8yrXATAy5G/zpF 1zmAEkTYIJ/ogUl+M2lhDsJVd0w5HCVARnPqsjvj6Az1nH6nMQcUkqh2jppxn6b8e/qR iaaDhIXCkzfi11JSAMm8IPxroP4Dsd/oDH1xv/btk0h3n27XPbnbLWJsJs5DkNpPs/9d BMGsL1jw7l20f1IozrG04xihtrd/tBfwEbEGHaWTjCvHZvuKnc48Mom6rHCwGhhcShDJ xAOZu73e2a2WkoRHWtJgXTXtoYddg1M0D6vexUdTGvZEuh9CgpmZ4aTWGfZJC11nlm9L Rung== X-Gm-Message-State: AA+aEWZ7bBtziRPBRkts93hGOnUPeR5/kw9jPR/khgyjS56s2ZxqWUpa WekVK41ehJ8H1YCzBtNyy65cFXKp X-Google-Smtp-Source: AFSGD/UZrFDqg1UJ5CRFIaZH1SUQ4MQRaxgFUtBquYf6j2M13um6AoOqvA7plLL5oj0oVyeTFgBMWw== X-Received: by 2002:a1c:91d1:: with SMTP id t200mr868062wmd.111.1543368901020; Tue, 27 Nov 2018 17:35:01 -0800 (PST) Received: from sunsetshimmer.lan ([2a02:8106:19:4900:3ab5:b1b6:c8c2:b39f]) by smtp.gmail.com with ESMTPSA id 80sm2031521wmv.6.2018.11.27.17.34.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 17:35:00 -0800 (PST) From: Tobias Schramm To: openwrt-devel@lists.openwrt.org Date: Wed, 28 Nov 2018 02:35:22 +0100 Message-Id: <20181128013523.5793-3-tobleminer@gmail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181128013523.5793-1-tobleminer@gmail.com> References: <20181128013523.5793-1-tobleminer@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181127_173513_321152_EBFD9DAA X-CRM114-Status: GOOD ( 13.16 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:343 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (tobleminer[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH v3 2/3] Replace use of blobmsg_check_attr by blobmsg_check_attr_safe X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tobias Schramm , nbd@nbd.name Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org blobmsg_check_attr_safe adds a length limit specifying the max offset from attr that can be read safely Signed-off-by: Tobias Schramm --- blobmsg.c | 24 ++++++++++++++++++------ blobmsg.h | 24 +++++++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/blobmsg.c b/blobmsg.c index 8019c45..10f3801 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -31,19 +31,29 @@ blobmsg_namelen(const struct blobmsg_hdr *hdr) return be16_to_cpu(hdr->namelen); } -bool blobmsg_check_attr(const struct blob_attr *attr, bool name) +bool blobmsg_check_attr_safe(const struct blob_attr *attr, bool name, size_t len) { const struct blobmsg_hdr *hdr; const char *data; - int id, len; + char *limit = (char*)attr + len; + int id, data_len; + + if (len < sizeof(struct blob_attr)) + return false; if (blob_len(attr) < sizeof(struct blobmsg_hdr)) return false; + if (len < sizeof(struct blobmsg_hdr)) + return false; + hdr = (void *) attr->data; if (!hdr->namelen && name) return false; + if ((char*)hdr->name + blobmsg_namelen(hdr) > limit) + return false; + if (blobmsg_namelen(hdr) > blob_len(attr) - sizeof(struct blobmsg_hdr)) return false; @@ -51,8 +61,10 @@ bool blobmsg_check_attr(const struct blob_attr *attr, bool name) return false; id = blob_id(attr); - len = blobmsg_data_len(attr); + data_len = blobmsg_data_len(attr); data = blobmsg_data(attr); + if (data_len < 0 || data + data_len > limit) + return false; if (id > BLOBMSG_TYPE_LAST) return false; @@ -60,7 +72,7 @@ bool blobmsg_check_attr(const struct blob_attr *attr, bool name) if (!blob_type[id]) return true; - return blob_check_type(data, len, blob_type[id]); + return blob_check_type(data, data_len, blob_type[id]); } int blobmsg_check_array(const struct blob_attr *attr, int type) @@ -111,7 +123,7 @@ int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len, blob_id(attr) != policy[i].type) continue; - if (!blobmsg_check_attr(attr, false)) + if (!blobmsg_check_attr_safe(attr, false, len)) return -1; if (tb[i]) @@ -158,7 +170,7 @@ int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, if (blobmsg_namelen(hdr) != pslen[i]) continue; - if (!blobmsg_check_attr(attr, true)) + if (!blobmsg_check_attr_safe(attr, true, len)) return -1; if (tb[i]) diff --git a/blobmsg.h b/blobmsg.h index c75f1d9..d17b896 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -104,7 +104,29 @@ static inline int blobmsg_len(const struct blob_attr *attr) return blobmsg_data_len(attr); } -bool blobmsg_check_attr(const struct blob_attr *attr, bool name); +/* + * blobmsg_check_attr_safe: safely validate a single untrusted attribute + * + * This method is a safe implementation of blobmsg_check_attr. + * It will limit all memory access performed on the blob to the + * range [attr, attr + len] (upper bound non inclusive) and is + * thus suited for checking untrusted blob attributes. + */ +bool blobmsg_check_attr_safe(const struct blob_attr *attr, bool name, size_t len); + +/* + * blobmsg_check_attr: validate a single attribute + * + * This method may be used with trusted data only. Providing + * malformed blobs will cause out of bounds memory access and + * crash your program or get your device 0wned. + */ +static inline bool +blobmsg_check_attr(const struct blob_attr *attr, bool name) +{ + return blobmsg_check_attr_safe(attr, name, blob_raw_len(attr)); +} + bool blobmsg_check_attr_list(const struct blob_attr *attr, int type); /* From patchwork Wed Nov 28 01:35:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tobias Schramm X-Patchwork-Id: 1004144 X-Patchwork-Delegate: blogic@openwrt.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) 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=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="LXJLXF8B"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="PMcDYl5L"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 434NX90TV4z9s1c for ; Wed, 28 Nov 2018 12:35:53 +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=va0NRXnGi2O0NlYuCcCa++Nsw96T4AYl0xU3r7ehvUQ=; b=LXJLXF8B0v8998 msX9WuTsvKrP1Nn7up4DmK7yCQfg86ff8UpNFdcEKhVz0f0af4Z6MrhkfUxVGpNcIIZPVw7y9tVar JmvzNCbZ9zMGg3aRYYnQ/AF+umouzObJJ76Bfp585COJ/+rWjFZfrc2RdqS0gfV4J0Ka0dsIYGwUv qOkeI0lHKqR29cwfdgWNRydXU4Kw3vCgdl5eWvj7076dFaKk6azcf7YScIrmwE8W6l6fnZEjslVYG yOUppMT5KsweVjwZB+nGwTCNn4EUkTbsm5I++68th3xXHhqrm3TYY3V8TJE/xpAxEEDI3b9tj5xjT TuWkxC9njX4NRug04mMA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRolV-0004EV-PB; Wed, 28 Nov 2018 01:35:45 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gRol0-0002Vn-6E for openwrt-devel@lists.openwrt.org; Wed, 28 Nov 2018 01:35:15 +0000 Received: by mail-wr1-x443.google.com with SMTP id t3so24623483wrr.3 for ; Tue, 27 Nov 2018 17:35:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+pX7eZvFWVKNyFbfM4amy/CZoKaEna/AZ+pej8ZIY6E=; b=PMcDYl5Lv8/6uWzxf/+7OG9l9ouT7e1llAoPEbIlWMtzWOaFJwzcSL9i5Rf0dcM5UW OJ9nOwQCj1c7wu2R6mJ0yQ7tEZW0rvtpg4YZwvCQeF2QHB20tegG437rNbQEvPj1EnBd vVqKwN2htQJSzrHa7l8Pqs11dH2FHyHDZJfpfjxjCk8SdNvY0D9FyAqkT7NQVOmqNmP2 Yo/r5YvuDIGfPMaD49xGUAJBzKFo8vMnv7bzQGcVgmkM+Y5h1zoPATbzaj1TG6iTswem 3bUD1AvQdVnSmeMH4aDUyUWYiblKHOIUzyp0EMHGJblDv3EPrWcu8smDMnSb7SFxPF3R ZTEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+pX7eZvFWVKNyFbfM4amy/CZoKaEna/AZ+pej8ZIY6E=; b=szAqtUW18rC0y7bxlUKJqCtzO9jrI0ObBbu30dhaZORgQz5XiyueiqMXCpHztJPvyE Mj8HIopQcfeZm13vM7EZXXZzzqp2FwM8K9uPi75xCOy8pdkRxiNWrszyigFeeUHJA+dq MGEttzIAnW1h4kIMm4hNzy2a8gsqYDWztQIsHg2/ndrLgJNXtObwZvwGiayJrpMx8t/Q j6VxuGgiRukqWoWwVMZ5WncIy9gwz3L3vv4bRBIBiC+M2UrNFN5UkU3/Q+m+CRzKNpxT FbrE0y61vKhG/zCrWSMNwuwIEBLfcbZcM0QCNDHjGObr2Fx3gWYTVG2BAimqzAzWwZdd Xg1g== X-Gm-Message-State: AA+aEWZU6LH0MU2fT1eS95RHjiEtLE9NdnwMJLD1LVSwBfkKbfAkhTqe KEvL/bjlWUxEcGW8jFrR4KQgz3es X-Google-Smtp-Source: AFSGD/VSdcy3oz8epWTdHy+WFAXkAHCtPFVMH+Fj7YAhpZ8xNxsDRQjCP2Y1ZIqJHDI+IRX96oou4A== X-Received: by 2002:adf:efd1:: with SMTP id i17mr15256122wrp.200.1543368902120; Tue, 27 Nov 2018 17:35:02 -0800 (PST) Received: from sunsetshimmer.lan ([2a02:8106:19:4900:3ab5:b1b6:c8c2:b39f]) by smtp.gmail.com with ESMTPSA id 80sm2031521wmv.6.2018.11.27.17.35.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Nov 2018 17:35:01 -0800 (PST) From: Tobias Schramm To: openwrt-devel@lists.openwrt.org Date: Wed, 28 Nov 2018 02:35:23 +0100 Message-Id: <20181128013523.5793-4-tobleminer@gmail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181128013523.5793-1-tobleminer@gmail.com> References: <20181128013523.5793-1-tobleminer@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181127_173514_225318_864C636F X-CRM114-Status: GOOD ( 12.37 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:443 listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (tobleminer[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain Subject: [OpenWrt-Devel] [PATCH v3 3/3] Add _safe variants for all attribute checking methods X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tobias Schramm , nbd@nbd.name Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org Signed-off-by: Tobias Schramm --- blobmsg.c | 12 ++++++++++-- blobmsg.h | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/blobmsg.c b/blobmsg.c index 10f3801..4b142e9 100644 --- a/blobmsg.c +++ b/blobmsg.c @@ -75,13 +75,16 @@ bool blobmsg_check_attr_safe(const struct blob_attr *attr, bool name, size_t len return blob_check_type(data, data_len, blob_type[id]); } -int blobmsg_check_array(const struct blob_attr *attr, int type) +int blobmsg_check_array_safe(const struct blob_attr *attr, int type, size_t len) { struct blob_attr *cur; bool name; int rem; int size = 0; + if (!blobmsg_check_attr_safe(attr, NULL, len)) + return -1; + switch (blobmsg_type(attr)) { case BLOBMSG_TYPE_TABLE: name = true; @@ -97,7 +100,7 @@ int blobmsg_check_array(const struct blob_attr *attr, int type) if (type != BLOBMSG_TYPE_UNSPEC && blobmsg_type(cur) != type) return -1; - if (!blobmsg_check_attr(cur, name)) + if (!blobmsg_check_attr_safe(cur, name, rem)) return -1; size++; @@ -111,6 +114,11 @@ bool blobmsg_check_attr_list(const struct blob_attr *attr, int type) return blobmsg_check_array(attr, type) >= 0; } +bool blobmsg_check_attr_list_safe(const struct blob_attr *attr, int type, size_t len) +{ + return blobmsg_check_array_safe(attr, type, len) >= 0; +} + int blobmsg_parse_array(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len) { diff --git a/blobmsg.h b/blobmsg.h index d17b896..81fa219 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -127,15 +127,62 @@ blobmsg_check_attr(const struct blob_attr *attr, bool name) return blobmsg_check_attr_safe(attr, name, blob_raw_len(attr)); } +/* + * blobmsg_check_attr_list: validate a list of attributes + * + * This method may be used with trusted data only. Providing + * malformed blobs will cause out of bounds memory access and + * crash your program or get your device 0wned. + */ bool blobmsg_check_attr_list(const struct blob_attr *attr, int type); +/* + * blobmsg_check_attr_list_safe: safely validate a list of untrusted attributes + * + * This method is a safe implementation of blobmsg_check_attr_list. + * It will limit all memory access performed on the blob to the + * range [attr, attr + len] (upper bound non inclusive) and is + * thus suited for checking untrusted blob attributes. + */ +bool blobmsg_check_attr_list_safe(const struct blob_attr *attr, int type, size_t len); + +/* + * blobmsg_check_attr: validate a list of attributes + * + * This methods may be used with trusted data only. Providing + * malformed blobs will cause out of bounds memory access and + * crash your program or get your device 0wned. + */ +bool blobmsg_check_attr_list(const struct blob_attr *attr, int type); + +/* + * blobmsg_check_array: safely validate untrusted array/table and return size + * + * Checks if all elements of an array or table are valid and have + * the specified type. Returns the number of elements in the array + * + * This method is a safe implementation of blobmsg_check_array. + * It will limit all memory access performed on the blob to the + * range [attr, attr + len] (upper bound non inclusive) and is + * thus suited for checking untrusted blob attributes. + */ +int blobmsg_check_array_safe(const struct blob_attr *attr, int type, size_t len); + /* * blobmsg_check_array: validate array/table and return size * * Checks if all elements of an array or table are valid and have * the specified type. Returns the number of elements in the array + * + * This method may be used with trusted data only. Providing + * malformed blobs will cause out of bounds memory access and + * crash your program or get your device 0wned. */ -int blobmsg_check_array(const struct blob_attr *attr, int type); +static inline int +blobmsg_check_array(const struct blob_attr *attr, int type) +{ + return blobmsg_check_array_safe(attr, type, blob_raw_len(attr)); +} int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len, struct blob_attr **tb, void *data, unsigned int len);