Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp1679731pxb; Thu, 7 Oct 2021 12:43:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx8IBKilJSag3YCoc7pjiAONMIX37+dhJJMdKK7GWjnon6vJc4EMkplXSaWSYmZDBA7ZfQ7 X-Received: by 2002:a17:903:11cd:b0:13e:596c:d9a7 with SMTP id q13-20020a17090311cd00b0013e596cd9a7mr5513778plh.37.1633635813032; Thu, 07 Oct 2021 12:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633635813; cv=none; d=google.com; s=arc-20160816; b=r3rl0VGJdi/5YkA53rlII2t7NNO1Cgb23MzIxHjmsT3qf2fZOh3oWQ5Q0AY/Owsx8m mz07uTHTU7aHSSvGNdxCu3n+ugEFKKqLFJTn3WgNTpOIHsW3JQgRFEjK1+jWSUl6+ZKV x75cB6IM5CqW9jFMuB/zH2pCBJBghSwTf6He+FLRonJg44nQoBSRybS9aEsS8VTZr/6H mF69FIh4EVuClCb0eO/ZxqpLF9gecOfFHFhWjTouJMmjFEHe5eLJeXWuGVooXB0Dvaw6 qJj/A5HHZH5X5+h2SFSjoG4eoeOXabS7pS9e4hftfAQCp2adv9iXdgQSL3tEj5G0PYQM hJxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=V2dvXNugsoPW/z2kbg9B0jr7MY21MWdNmDDiosCNYNo=; b=BLHT145pWfc0CvGK51jcDGStOJ2/ojFSZ/IoIA8LHfGC6wzuqFRtTV1EMYyO2DdjGH fuIi5ByEt5pGnmLfSqYXsN/Dr7t2EZ+0GnIIJg3u/Q84Q5CD2LiZqA5kHSziWEfN8/Kz Jjyo4M7CwMJIkHCtj8Mu44Y8u9kCz9WmOIsNXuRHVthi1yMbgktF9jAP/ArdVnPqrEWj DRwHOhsFMsnPUMJUDHuUlsd9hFJUtZSEP2Fl6HLkML5SFn13gXRDG9pTYRfrYhH/yLC8 JCKekwsOD/irTWwemkW1worQdqDfr5obyZ+oGMmObLPxDmivAfwK1jx9zQ6mwYp67FNv JGYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=NRrjMl40; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u36si273763pgl.231.2021.10.07.12.43.20; Thu, 07 Oct 2021 12:43:33 -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=@gmail.com header.s=20210112 header.b=NRrjMl40; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243179AbhJGRPK (ORCPT + 99 others); Thu, 7 Oct 2021 13:15:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243173AbhJGRPJ (ORCPT ); Thu, 7 Oct 2021 13:15:09 -0400 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6533BC061570 for ; Thu, 7 Oct 2021 10:13:15 -0700 (PDT) Received: by mail-ed1-x52a.google.com with SMTP id l7so25886596edq.3 for ; Thu, 07 Oct 2021 10:13:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=V2dvXNugsoPW/z2kbg9B0jr7MY21MWdNmDDiosCNYNo=; b=NRrjMl40Ai5Wwg6r2HfzNaX7KgC9suCtZTXy6M/Wo51m82Jd2SEJcBGUHrWDLwwD/k DOr6dbyEz7eIU1z1djoGaDiRZKbd8buR4Ju9r19VjcX+uN548VOLmwFzQR4392usqwpg zytpMv2YLaUtzOaixFOnuRvmisvWHj2sr4goOKJzlm5B+XFddwPmgwdBG1RtXqp3xycJ 0wddOD5iNkk0pHTcOjU76tc1TvpNoaQmCC53F9BB9u1zDGwYACO/PqQxLGg9dZP1QUvx jEnZJojs0dK0DlZPIEoBLeTl1gEVY8M8HgJH5NeWVpUGU/WARR17Mrf2wjCSd18gKlWJ rrRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=V2dvXNugsoPW/z2kbg9B0jr7MY21MWdNmDDiosCNYNo=; b=q0HqX4oUD9Gnmweodd+aD6eEXJNdnE5VuKt9BF2t4rAr8hmcKbZ+/2nzNmF/kqnzHN +gQE9Dj9nj89PIiXDcMoIBv4o3EsFPP6Gn5Rr0UgtCWOKd960DcySJxZFXKgcwiEf167 mJbYwl4LiBN1d1akoQEswPXW2UQzovCSIP8wqSYE5BPbmw9/8YsgtsN3ISgMmsP06bM0 A/4XLVReEBdT7bXDEZ1ZeW28oz/wvVT3ORm/LYiPajWRUPAA3VHvJVcUbu7/LMWLOKjI 27LavUjDtIKadSK0dDgKEN0TTHVvRGg2ERtxM+AtI/WORsU+2kMC+NKrKpvNWjPHLlDm EGqg== X-Gm-Message-State: AOAM531NNDYP/1LxlROmCXq85kkfMPawL0mUu4h2cLm0SoRI40G72Ayc wzOHiXT50g+L43uqU/V32H0mZgym0g== X-Received: by 2002:a05:6402:5252:: with SMTP id t18mr7605932edd.129.1633626794018; Thu, 07 Oct 2021 10:13:14 -0700 (PDT) Received: from localhost.localdomain ([46.53.254.50]) by smtp.gmail.com with ESMTPSA id c11sm21772edw.5.2021.10.07.10.13.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Oct 2021 10:13:13 -0700 (PDT) Date: Thu, 7 Oct 2021 20:13:11 +0300 From: Alexey Dobriyan To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Kees Cook Subject: Re: [PATCH] ELF: fix overflow in total mapping size calculation Message-ID: References: <20211005172129.4092cc4148bbcac36a128b55@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20211005172129.4092cc4148bbcac36a128b55@linux-foundation.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 05, 2021 at 05:21:29PM -0700, Andrew Morton wrote: > On Sun, 3 Oct 2021 15:11:24 +0300 Alexey Dobriyan wrote: > > > Kernel assumes that ELF program headers are ordered by mapping address, > > but doesn't enforce it. It is possible to make mapping size extremely huge > > by simply shuffling first and last PT_LOAD segments. > > > > As long as PT_LOAD segments do not overlap, it is silly to require > > sorting by v_addr anyway because mmap() doesn't care. > > > > Don't assume PT_LOAD segments are sorted and calculate min and max > > addresses correctly. > > It sounds good, but why do I have the feeling this will explode in some > unexpected fashion? Because it's elf, and that's what it does :( Good news, it is ELF, we'll hear about breakage immediately. :^) Kernel "enforces" PT_LOAD ordering: if total mapping size overflows, then mmap will reject it. I hope every ELF binary maintains ordering. But! total_mapping_size() only looks at first and the last PT_LOAD segments which is obviously incorrect.