Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3311432imu; Sun, 11 Nov 2018 12:07:37 -0800 (PST) X-Google-Smtp-Source: AJdET5cQ39h3H/uTK/7fPmvXWBr6BzbO0XtC/t+nzHo4Lb7rIC3/Qq5+iGhajXtg3ctZNP3HkpdJ X-Received: by 2002:a63:ec13:: with SMTP id j19mr977158pgh.6.1541966857097; Sun, 11 Nov 2018 12:07:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541966857; cv=none; d=google.com; s=arc-20160816; b=B96eBnA7n3HWkloqppPtL/g9S0IbZ99EvwtYvaMY9ExjrDA1hR+g5Hd275CDEFtuv8 S+bz6uC8bFH0vnJsgEYvrEogB23ha/ULgv1JPPhAJNvbArrioJVw3j1s4hQcp7mTvkdc iVmbJo0llNTWjC5hZF1I7GWKOQTLoQThGHtNzv0uOJu2jOgOA04bsmVQOYAKr+1OdF6w qkrx96eTZyoHjvgJ5PQp4qH4i178yh3wyrKQMsRx/cvfhhLQiAvOW3e5MCFoLPuCpPxY ALBN4MmWOA9+P4y0T+Uxe45q4hvtxcTC4QsHqqJaFYJm0ICMmQAPacg3QTR/O+MB8GA/ 3sug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition; bh=/IeT6xz/8Ny/6i+nEP6VE0mjbwnxB5w2fsmMsQkIpfI=; b=nPUIQ9ZcIpSSBTswy24z0fJHmFLfSrDVVDfNcfBNBfUFpvdtQF4JlkurmD1APP+Qsg iIip1YWmp0bWQB/GUv2cVWHWaczh2Wky6o9G+0Zrv9g2WxLnpqFifHRgZjZkukbeXDiQ jGNu319eVCG81AwTw0LUvK4nNUGK+XeLlSPqZC+fUe1MWMWj/Dw1NUb1mzbkcMSz1CmO VltNaSsMjc3xEQP0r6qh+uRZ4udh7UvZ7GXromiyjYZjrsXhUSybvXTxaJ+BVOAncchU 2nsvwnOZSZJaNI+I74jI3XOzZSITcUNu+ZC7+0nW2qOio36Gjt4Us7YQ58aOtjSsgFl1 /q+Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n1-v6si14925333pld.378.2018.11.11.12.07.21; Sun, 11 Nov 2018 12:07:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731421AbeKLF4a (ORCPT + 99 others); Mon, 12 Nov 2018 00:56:30 -0500 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:52062 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727449AbeKLF43 (ORCPT ); Mon, 12 Nov 2018 00:56:29 -0500 Received: from [192.168.4.242] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gLvt3-0000oO-DZ; Sun, 11 Nov 2018 19:59:13 +0000 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1gLvsR-0001X7-OO; Sun, 11 Nov 2018 19:58:35 +0000 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Linus Torvalds" , "Thadeu Lima de Souza Cascardo" , "Alexander Viro" Date: Sun, 11 Nov 2018 19:49:05 +0000 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 127/366] fs/binfmt_misc.c: do not allow offset overflow In-Reply-To: X-SA-Exim-Connect-IP: 192.168.4.242 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.61-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Thadeu Lima de Souza Cascardo commit 5cc41e099504b77014358b58567c5ea6293dd220 upstream. WHen registering a new binfmt_misc handler, it is possible to overflow the offset to get a negative value, which might crash the system, or possibly leak kernel data. Here is a crash log when 2500000000 was used as an offset: BUG: unable to handle kernel paging request at ffff989cfd6edca0 IP: load_misc_binary+0x22b/0x470 [binfmt_misc] PGD 1ef3e067 P4D 1ef3e067 PUD 0 Oops: 0000 [#1] SMP NOPTI Modules linked in: binfmt_misc kvm_intel ppdev kvm irqbypass joydev input_leds serio_raw mac_hid parport_pc qemu_fw_cfg parpy CPU: 0 PID: 2499 Comm: bash Not tainted 4.15.0-22-generic #24-Ubuntu Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.11.1-1 04/01/2014 RIP: 0010:load_misc_binary+0x22b/0x470 [binfmt_misc] Call Trace: search_binary_handler+0x97/0x1d0 do_execveat_common.isra.34+0x667/0x810 SyS_execve+0x31/0x40 do_syscall_64+0x73/0x130 entry_SYSCALL_64_after_hwframe+0x3d/0xa2 Use kstrtoint instead of simple_strtoul. It will work as the code already set the delimiter byte to '\0' and we only do it when the field is not empty. Tested with offsets -1, 2500000000, UINT_MAX and INT_MAX. Also tested with examples documented at Documentation/admin-guide/binfmt-misc.rst and other registrations from packages on Ubuntu. Link: http://lkml.kernel.org/r/20180529135648.14254-1-cascardo@canonical.com Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Thadeu Lima de Souza Cascardo Reviewed-by: Andrew Morton Cc: Alexander Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: - Error label is "Einval" - Adjust context] Signed-off-by: Ben Hutchings --- fs/binfmt_misc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) --- a/fs/binfmt_misc.c +++ b/fs/binfmt_misc.c @@ -319,8 +319,13 @@ static Node *create_entry(const char __u char *s = strchr(p, del); if (!s) goto Einval; - *s++ = '\0'; - e->offset = simple_strtoul(p, &p, 10); + *s = '\0'; + if (p != s) { + int r = kstrtoint(p, 10, &e->offset); + if (r != 0 || e->offset < 0) + goto Einval; + } + p = s; if (*p++) goto Einval; e->magic = p; @@ -341,7 +346,8 @@ static Node *create_entry(const char __u if (e->mask && string_unescape_inplace(e->mask, UNESCAPE_HEX) != e->size) goto Einval; - if (e->size + e->offset > BINPRM_BUF_SIZE) + if (e->size > BINPRM_BUF_SIZE || + BINPRM_BUF_SIZE - e->size < e->offset) goto Einval; } else { p = strchr(p, del);