From patchwork Wed Nov 20 11:59:22 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: 1198075 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="WDAjj/Qt"; 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 47J1VR3ghpz9sR7 for ; Wed, 20 Nov 2019 23: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=3YAY1ZsM3m7tdkuAeIyTWcpNF/e0gL8jVOZFEHzpjcU=; b=WDAjj/QtvRObt9 jdMEEEIx3Wxa5Jv3fduwdvtiMpyxW+uOX5AV0MN+lzOaeKJiMD/d41BRFcHAvVMhY44NJcS/5wXwg rk8HHP2N+t1LT03fIzKHaRidS/w6EyB3CY0ASM1j3dQ5ceHSzGUk+7Y5j4eXrxl/K9S0UAm+QyNH5 aXRIPALKhoJMMoNgr4NTFM+txlVwVjXqW5kkL6izfyjuwvO/4UQGpXt3cpvVhA0paidJeww0pdALc ofm1rMB56+xhL7cSRVlyIYqK1/KVYE9acPIGvukSVWsUso97zVvy/wUe8kLzBU6ZFAcKSJkJPvhhL HRg6elUitAxa+GJULfCA==; 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 1iXOez-0003zJ-Ju; Wed, 20 Nov 2019 12:00:37 +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 1iXOe5-0001Zc-2r for openwrt-devel@lists.openwrt.org; Wed, 20 Nov 2019 11:59:42 +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 45DAA466D; Wed, 20 Nov 2019 12:59:32 +0100 (CET) Received: by meh.true.cz (OpenSMTPD) with ESMTP id c91570d9; Wed, 20 Nov 2019 12:59:22 +0100 (CET) From: =?utf-8?q?Petr_=C5=A0tetiar?= To: openwrt-devel@lists.openwrt.org Date: Wed, 20 Nov 2019 12:59:22 +0100 Message-Id: <20191120115926.23272-6-ynezz@true.cz> In-Reply-To: <20191120115926.23272-1-ynezz@true.cz> References: <20191120115926.23272-1-ynezz@true.cz> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191120_035941_302725_D156B436 X-CRM114-Status: UNSURE ( 7.57 ) 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_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record Subject: [OpenWrt-Devel] [PATCH libubox 5/9] base64: b64_decode: fix possible null pointer dereference 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 clang-10 analyzer reports following: base64.c:325:20: warning: Array access (from variable 'target') results in a null pointer dereference target[tarindex] = 0; ~~~~~~ ^ and prepared test case confirms it: Invalid write of size 1 at 0x4E4463F: b64_decode (base64.c:325) by 0x40088C: test_invalid_inputs (tests/test-base64.c:26) by 0x40088C: main (tests/test-base64.c:32) Address 0x1 is not stack'd, malloc'd or (recently) free'd Process terminating with default action of signal 11 (SIGSEGV) Access not within mapped region at address 0x1 at 0x4E4463F: b64_decode (base64.c:325) by 0x40088C: test_invalid_inputs (tests/test-base64.c:26) by 0x40088C: main (tests/test-base64.c:32) Signed-off-by: Petr Štetiar --- base64.c | 6 ++++++ tests/test-base64.c | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/base64.c b/base64.c index 4759ede01e7c..d9f4112179f3 100644 --- a/base64.c +++ b/base64.c @@ -144,6 +144,9 @@ int b64_encode(const void *_src, size_t srclength, u_char output[4]; size_t i; + if (!dest || targsize == 0) + return -1; + while (2 < srclength) { input[0] = *src++; input[1] = *src++; @@ -208,6 +211,9 @@ int b64_decode(const void *_src, void *dest, size_t targsize) state = 0; tarindex = 0; + if (!dest || targsize == 0) + return -1; + while ((ch = (unsigned char)*src++) != '\0') { if (isspace(ch)) /* Skip whitespace anywhere. */ continue; diff --git a/tests/test-base64.c b/tests/test-base64.c index c29b4e2c73a3..415fc5969c53 100644 --- a/tests/test-base64.c +++ b/tests/test-base64.c @@ -17,8 +17,23 @@ static void test_b64_decode(const char *src) fprintf(stdout, "%d %s\n", r, dst); } +static void test_invalid_inputs() +{ + b64_decode(NULL, NULL, 0); + b64_decode("Zg==", NULL, 0); + b64_decode("Zg==", NULL, 2); + + b64_encode(NULL, 0, NULL, 2); + b64_encode(NULL, 2, NULL, 2); + b64_encode("foo", 3, NULL, 2); + b64_encode("foo", 3, NULL, 0); +} + int main() { + + test_invalid_inputs(); + test_b64_encode(""); test_b64_encode("f"); test_b64_encode("fo");