Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp164045pxa; Wed, 26 Aug 2020 07:30:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw5qrSgA+jK1NKhHublWdzs7xtfd0adTvknm4ck5PAhpHOfKYnkHcuf3MTi4Tf4rElvWaCo X-Received: by 2002:a17:906:4047:: with SMTP id y7mr15569095ejj.21.1598452202642; Wed, 26 Aug 2020 07:30:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598452202; cv=none; d=google.com; s=arc-20160816; b=jAogL1ec/jxpcpz/Tb5kdGxzKEGTZfv/feBsd/Ao3YKT8p2UTRw/T3hoU71RyXD4LW Ydu9NZoc43/nTyHNOtUivTiT5rH+Wqwo4LoL9Cuud5IKC/u2u+T1ENpfQiWZ7Com/6zB tULBkabiVmy2SqZX+VOXIIdlq79o/FEjnwg+wIY+EjLBz08djqCKbvG1af5lVyr4tLNa C67LycmE4EP1G1S+tnZAZvCfpOpb8MU6fnvuidrMJNDMypoywIJU3Tcp7qvdCPMAmm36 aiWZx+HEO1k7M9zFwMOj7Ieu8BmCmKz5yDTC1XdKfnyTqG10hB17aMUI9w4KFjdKQWaw +VqQ== 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=gMLYSa4AyGl9IsgE/xp9/uBC2Hj1lHh3vIGEy1YNAmw=; b=WlWe2H/bxn6pRdGZLlQtRAm1+AflmfExjTfkq4KWadWoi7ygvyl8R5yTuOs2sqFzq1 B/p511dUHx2aFFsId6gqHQusR158gwgAAQ5q8yOZ8MHXOnS8XujTNyvF2wQvwLrnYt7g c+5cig3I6LTMNuEbTAATf1cRQQ4LDZ0knY06b/wywxkP6Nfbd1/e/bVFhi/yrT2lstV5 JgVAXof3jyTCgwjKhlf4X/zlYSEeYoV0COHgB629rRr22qxa/osu8vOAoSDXcyqN6D0p dStfRq4XKLhTGvrITscHa+wVZLfoyzd1B7Yh0j4QjGKJYPS3OoiRN9c3jFzpsVfDiLFH 4n/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1lU78uE6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u18si1727817eji.296.2020.08.26.07.29.39; Wed, 26 Aug 2020 07:30:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=1lU78uE6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729621AbgHZMEW (ORCPT + 99 others); Wed, 26 Aug 2020 08:04:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:37886 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729501AbgHZMDI (ORCPT ); Wed, 26 Aug 2020 08:03:08 -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 8B7C020838; Wed, 26 Aug 2020 12:03:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598443388; bh=MqAyc3vVX6Sn4o+E+TZtYRzJyGODsIk2bdb5U1rmkkw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1lU78uE67qOwZhtahGsII2fUoD+L1gAuTKk1xaJorQdjIR02Ka/wvXuq27Sbu5/G7 zljhyxovSTNt6l2rblc1599USUaunc30XhYyyHAyFrDgGRJCh1HImj6joF7ZPCAyZK 2A3eUX0FdlGB6bixXUa2bTjaRBoVqgJVlgxk6Rsk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Max Filippov , Greg Ungerer Subject: [PATCH 5.7 15/15] binfmt_flat: revert "binfmt_flat: dont offset the data start" Date: Wed, 26 Aug 2020 14:02:43 +0200 Message-Id: <20200826114850.032392369@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200826114849.295321031@linuxfoundation.org> References: <20200826114849.295321031@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: Max Filippov commit 2217b982624680d19a80ebb4600d05c8586c4f96 upstream. binfmt_flat loader uses the gap between text and data to store data segment pointers for the libraries. Even in the absence of shared libraries it stores at least one pointer to the executable's own data segment. Text and data can go back to back in the flat binary image and without offsetting data segment last few instructions in the text segment may get corrupted by the data segment pointer. Fix it by reverting commit a2357223c50a ("binfmt_flat: don't offset the data start"). Cc: stable@vger.kernel.org Fixes: a2357223c50a ("binfmt_flat: don't offset the data start") Signed-off-by: Max Filippov Signed-off-by: Greg Ungerer Signed-off-by: Greg Kroah-Hartman --- fs/binfmt_flat.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) --- a/fs/binfmt_flat.c +++ b/fs/binfmt_flat.c @@ -571,7 +571,7 @@ static int load_flat_file(struct linux_b goto err; } - len = data_len + extra; + len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long); len = PAGE_ALIGN(len); realdatastart = vm_mmap(NULL, 0, len, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0); @@ -585,7 +585,9 @@ static int load_flat_file(struct linux_b vm_munmap(textpos, text_len); goto err; } - datapos = ALIGN(realdatastart, FLAT_DATA_ALIGN); + datapos = ALIGN(realdatastart + + MAX_SHARED_LIBS * sizeof(unsigned long), + FLAT_DATA_ALIGN); pr_debug("Allocated data+bss+stack (%u bytes): %lx\n", data_len + bss_len + stack_len, datapos); @@ -615,7 +617,7 @@ static int load_flat_file(struct linux_b memp_size = len; } else { - len = text_len + data_len + extra; + len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32); len = PAGE_ALIGN(len); textpos = vm_mmap(NULL, 0, len, PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0); @@ -630,7 +632,9 @@ static int load_flat_file(struct linux_b } realdatastart = textpos + ntohl(hdr->data_start); - datapos = ALIGN(realdatastart, FLAT_DATA_ALIGN); + datapos = ALIGN(realdatastart + + MAX_SHARED_LIBS * sizeof(u32), + FLAT_DATA_ALIGN); reloc = (__be32 __user *) (datapos + (ntohl(hdr->reloc_start) - text_len)); @@ -647,9 +651,8 @@ static int load_flat_file(struct linux_b (text_len + full_data - sizeof(struct flat_hdr)), 0); - if (datapos != realdatastart) - memmove((void *)datapos, (void *)realdatastart, - full_data); + memmove((void *) datapos, (void *) realdatastart, + full_data); #else /* * This is used on MMU systems mainly for testing. @@ -705,7 +708,8 @@ static int load_flat_file(struct linux_b if (IS_ERR_VALUE(result)) { ret = result; pr_err("Unable to read code+data+bss, errno %d\n", ret); - vm_munmap(textpos, text_len + data_len + extra); + vm_munmap(textpos, text_len + data_len + extra + + MAX_SHARED_LIBS * sizeof(u32)); goto err; } }