Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp3072543imm; Sun, 1 Jul 2018 11:36:22 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcM+IYCPXscYsmSvhpyKTsgyABaxOv7yvaMKy4j27RFTdzl2zCHvNuZul5lcR7wr3wK2G1Y X-Received: by 2002:a63:f919:: with SMTP id h25-v6mr325302pgi.401.1530470182167; Sun, 01 Jul 2018 11:36:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530470182; cv=none; d=google.com; s=arc-20160816; b=m1ADqvmUD5BqusYOauGAmzd0njEq4oGkTd88p/o4DVbUvdTe3b1uJLv1A1mSZh3TQy kX9xlD7y6pHEeto/htZ01G61dTOCEIxO/GR+SlzkP8CKfzGvgZDo57OytATdXRqoQNHf eZZlkB7DSDFTwraF3bBky29wUiJ3zD1fLd6gAZibnfISGX2iiUY2Exi7/ecQLkqXQ10m Ttcmfjy2h3zVyKM8f0IRPeAkPfTvmsIZpPxpAhGHxpIWexZynx4lsGdzrECAZE/XpF2o ksVbH/8Db7xAEkPojCa+IN/ghzOIMYCeykEvKxHpzlTQi00dUERPVkF0xD1IIRzunnr3 jr1g== 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=CIdqqVDh8BpyoefdhwXZLHKlpaGopmckfL9SOqK6X3M=; b=FVKnwI8dkJUluz4H9PZY6jL/AB46uNgt+g9KEjNbCCZRNP/gpcDRS+OGL+Fk3xV9Vg ktNbMc1RFRV77vgwMhNCAAdP0zLouovK9zp9oZ0rQtm7M0xilzRf9dHmouUopw0GwyXO 5//U6EOHMZC/8IIbiNWnqpDimRXUtTe+WGBTnIx/+lECWGMldC2ijr6OibTPzhXczPOb gt5UdGvBBBq3j15nPbxJThNDgDhqi8s5USi/S3hbS7EFjonuu8hisbRfGUSTYbp+JD3R eXfOm11ULXBSeaMB515B/DHJg8gB6GeKNfooOseuR07ULHqwPNHwWpqZd5IGrcDqD5UP 544w== 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 u7-v6si12827952pgn.194.2018.07.01.11.36.08; Sun, 01 Jul 2018 11:36:22 -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 S933639AbeGASet (ORCPT + 99 others); Sun, 1 Jul 2018 14:34:49 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:60672 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933048AbeGAQP6 (ORCPT ); Sun, 1 Jul 2018 12:15:58 -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 65D2886D; Sun, 1 Jul 2018 16:15:57 +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 4.4 025/105] fs/binfmt_misc.c: do not allow offset overflow Date: Sun, 1 Jul 2018 18:01:35 +0200 Message-Id: <20180701153151.172458466@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180701153149.382300170@linuxfoundation.org> References: <20180701153149.382300170@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 4.4-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 @@ -369,8 +369,13 @@ static Node *create_entry(const char __u 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; pr_debug("register: offset: %#x\n", e->offset); @@ -410,7 +415,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; pr_debug("register: magic/mask length: %i\n", e->size); if (USE_DEBUG) {