Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp3078479imm; Sun, 1 Jul 2018 11:46:00 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIdJ3GW2Ker0qWBV0AipRx43cdG/GezpNs0sKRrnJQzzQ+2zAQTR+sBSQ5zYRnuGso2kAZt X-Received: by 2002:a65:4783:: with SMTP id e3-v6mr19396925pgs.235.1530470760729; Sun, 01 Jul 2018 11:46:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530470760; cv=none; d=google.com; s=arc-20160816; b=wujZifHR1j/D9Km+1Khe+weJZeomkUSbowDxBLlPpKHcPs5f+sJoPKEr0LCncCIvny NskY5SCXJS1XBG03HGkzuXD6noilQOQAXVFi5tG0+Oi2uh0xggZqdhZBmkmR84aAmKFX ZtA0c/db+OIBUmuFZnR1JRPbTrnotmi+cfBKyirtnrr8mtgXKKx3HdoWg6trHsbR2UHW lPkEAUDRpUrx5bxB8MWaBdeJs9OQaZxc/Iii9fSGJi1DSOMVjgNeLYnN9Ktm064AO8Sx 7q/MZvLgxfr3BHNwJ+C6Ej+KvSpGZpDN+9TIAASEeVU1SAfRpaeHhWkZxoC8FClx1/I/ ahTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=wYNV0Jbao8WBwYWdX6vO0stJdwT64Qbh+2LHw4YvqOs=; b=iv8oj1W4KvmMhVI6vp5IIqR9jpd9yvnt52o3P4/n0STLfFWuAsiBSTR1hh6OPlutyz TZ7o6G8b+L+r/faNCnzMlVygPu84TDfKyDQuzL4wo0rCn0/FWJ0Z0gpt/9DPh5Nq+/4O zpDB1YIKiyX+v2mBkWC7jde7Kqxy110rPWhBO2MfhAPtaiHU/2wPaDzUFNrCAHIMw6BM mHLGZV2r7G0arD9oqtDvD5YDMkQXWdzaYz/gT47GP4keLs8SI9aKE/yrXApg8Yh6NMTi WhxKmYVxIBUb57kD4FREd6P0lJwpwn6DobOWFzkoKbITkKzfJBBl6FYAGJBxP/JLAe6M E7/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 d5-v6si14086134plo.3.2018.07.01.11.45.46; Sun, 01 Jul 2018 11:46:00 -0700 (PDT) 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 S932273AbeGAQNC (ORCPT + 99 others); Sun, 1 Jul 2018 12:13:02 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:60032 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752735AbeGAQM4 (ORCPT ); Sun, 1 Jul 2018 12:12:56 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D9DDC4A3; Sun, 1 Jul 2018 16:12:55 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thadeu Lima de Souza Cascardo , Andrew Morton , Alexander Viro , Linus Torvalds Subject: [PATCH 3.18 46/85] fs/binfmt_misc.c: do not allow offset overflow Date: Sun, 1 Jul 2018 18:02:04 +0200 Message-Id: <20180701153124.191066805@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180701153122.365061142@linuxfoundation.org> References: <20180701153122.365061142@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable 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 Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/binfmt_misc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) --- a/fs/binfmt_misc.c +++ b/fs/binfmt_misc.c @@ -334,8 +334,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; @@ -356,7 +361,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);