Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7148117ybi; Mon, 8 Jul 2019 15:36:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwzTJbE2fezoSc9FCypfDtfmJuQ565kn2pBdsWidzX/zStNYweDS0nb4d77yvAFQqtQqzyk X-Received: by 2002:a63:e610:: with SMTP id g16mr15325081pgh.392.1562625391624; Mon, 08 Jul 2019 15:36:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562625391; cv=none; d=google.com; s=arc-20160816; b=Fd7xU1ftktDOu9VnPOmyst2O5FQZB6xrMxel8/gvdy/tkcPN5S9KWBDz3CiTWIeOqm NH5ZyH7obJIpSxOj9MG9EM8SwXxDM5IDb0TQcnuEveP9Kb/5fYThtrqL4cqRG0qAjaKB o8OyZedMS/YOD0CgIdlvQ7eg4v7x7zqEsOHICKiQS0UOPrh/OiQRDHV3TdbKhAtXOrLZ Vz6/M+i7m3j2px3btGvYsbBSb0X6TOorcCZ0wY+Bn8D7mDiU8syL+Con69uIUr3N/NEq ZCijWiqYwY/IhgeUwOpGjQyksO9SfZxTnbAdKvp1ddpHqC2x3laBwzwlFKJ9x4jNITtG IFEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=tKK2gWNLeEFsdJcTcKVaw1Y8T4DBjtPOHHpaeXpkAMQ=; b=SWopvzSpEV+stjAvj/pzmcTUDjqV1kxdU0e8QsgV6LE+7mr6KWqOGy/w/jxVn3OhY2 SocFUTuHWXqzdqOibL6e03ss/2EywK+CSvt3KU1RikowYaR3pe/s1wa2COshWxXr71K3 IfHHb8xQzbuRgRgkwTsYpOVRt3Qr50kY+DX6JYuxQzyxPsDboOFVNe8ENe2oHU6boM06 JJrCaP27Q8J3EKXx0nnSmleGFF2mAVTiqg6yVQ6DJZmBRQ0g8ZfN0iBB5ABgv/wk4c05 8pxjBfh4IizRQmTCzRZOD+sOwLv+KyeXqO6jjrIljwC6qanAdEF0lV2sK3LVGC9KK7N7 PH7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=b2MdASjD; 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 x8si19868685pfa.186.2019.07.08.15.36.16; Mon, 08 Jul 2019 15:36:31 -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; dkim=pass header.i=@kernel.org header.s=default header.b=b2MdASjD; 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 S2388011AbfGHPWi (ORCPT + 99 others); Mon, 8 Jul 2019 11:22:38 -0400 Received: from mail.kernel.org ([198.145.29.99]:49080 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387995AbfGHPWd (ORCPT ); Mon, 8 Jul 2019 11:22:33 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2C75B21738; Mon, 8 Jul 2019 15:22:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1562599351; bh=9YKzwNqaITT/3dAfxQk+d2f6hPVO62jHvjPHCwJDh/k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b2MdASjDjbpvGuilb8ZPqHrI2tX2NOY5IYPbq2iWgGSTJOjSi88jprbT851cMMZbr L7YonmHZXhW1pxb7Xz5AgL0gpuXM0cabvNue0wd+g3qsqGbSbNw2N1vVqLxtiJkM+s B1h8lj5YgHCLqx97zilJiO86sSHA3owrIBFFrbuY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Alexander Viro , Kees Cook , Nicolas Pitre , Arnd Bergmann , Geert Uytterhoeven , Russell King , Greg Ungerer , Andrew Morton , Linus Torvalds Subject: [PATCH 4.9 046/102] fs/binfmt_flat.c: make load_flat_shared_library() work Date: Mon, 8 Jul 2019 17:12:39 +0200 Message-Id: <20190708150528.813960180@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190708150525.973820964@linuxfoundation.org> References: <20190708150525.973820964@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn commit 867bfa4a5fcee66f2b25639acae718e8b28b25a5 upstream. load_flat_shared_library() is broken: It only calls load_flat_file() if prepare_binprm() returns zero, but prepare_binprm() returns the number of bytes read - so this only happens if the file is empty. Instead, call into load_flat_file() if the number of bytes read is non-negative. (Even if the number of bytes is zero - in that case, load_flat_file() will see nullbytes and return a nice -ENOEXEC.) In addition, remove the code related to bprm creds and stop using prepare_binprm() - this code is loading a library, not a main executable, and it only actually uses the members "buf", "file" and "filename" of the linux_binprm struct. Instead, call kernel_read() directly. Link: http://lkml.kernel.org/r/20190524201817.16509-1-jannh@google.com Fixes: 287980e49ffc ("remove lots of IS_ERR_VALUE abuses") Signed-off-by: Jann Horn Cc: Alexander Viro Cc: Kees Cook Cc: Nicolas Pitre Cc: Arnd Bergmann Cc: Geert Uytterhoeven Cc: Russell King Cc: Greg Ungerer Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/binfmt_flat.c | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) --- a/fs/binfmt_flat.c +++ b/fs/binfmt_flat.c @@ -859,9 +859,14 @@ err: static int load_flat_shared_library(int id, struct lib_info *libs) { + /* + * This is a fake bprm struct; only the members "buf", "file" and + * "filename" are actually used. + */ struct linux_binprm bprm; int res; char buf[16]; + loff_t pos = 0; memset(&bprm, 0, sizeof(bprm)); @@ -875,25 +880,11 @@ static int load_flat_shared_library(int if (IS_ERR(bprm.file)) return res; - bprm.cred = prepare_exec_creds(); - res = -ENOMEM; - if (!bprm.cred) - goto out; - - /* We don't really care about recalculating credentials at this point - * as we're past the point of no return and are dealing with shared - * libraries. - */ - bprm.cred_prepared = 1; - - res = prepare_binprm(&bprm); + res = kernel_read(bprm.file, pos, bprm.buf, BINPRM_BUF_SIZE); - if (!res) + if (res >= 0) res = load_flat_file(&bprm, libs, id, NULL); - abort_creds(bprm.cred); - -out: allow_write_access(bprm.file); fput(bprm.file);