Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp605741pxb; Mon, 16 Aug 2021 12:51:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxH6MJbMIt1zudYrhGUjTDgsOXjzvQza5xYsgSflJh1nzPgdv1SUQSMa7NcKw3PROh/djP0 X-Received: by 2002:a05:6402:1d1c:: with SMTP id dg28mr289327edb.234.1629143466106; Mon, 16 Aug 2021 12:51:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629143466; cv=none; d=google.com; s=arc-20160816; b=xpHovaiYhpriSeUARlxNp0AyJ4FXdKHJCkBXLey3bMRSsWCYAzn81m3eoGRQV88NET UkBGg1LtTEH9JMhcYhTDQ3NCVLoKQRHV3fuQWxzWcGn/6yfLqlUtzg8K3vzkVy0vbKwP S8vnUKno5zHeo1QCLWB7iIOp94oroHQfTc5zid4gsQlVLvH2jVHBAMNwCafHNGfiMZiE U+vH2cNWOhzg7t1agpB9fTGEaEqJHfii1uIY0OS4a8zIacBDGJk2/em8qkZt2MGmanrI Hbii4g54JWy0JXnIy/8P1vNrhDOH1iDBn4yjLkn97c2t8+fngnWfVjh4jxyjTpqUy4mC 6EeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=celAbCKZabJZZFRZffcKhJ6LMDssZFIlnWwC0cSAxUw=; b=RoaEFasB77vuETDPDeHFCcHhyK04POkqksTkhMCqEEfE2kZdgJ0W05fs7bxlw2CUQS wp6E0aO6yg4VgGF7ucaLp0rVj2x/l7pTx6f+hBR4THd1dkWNX+NUpkVUV0jffbfaY2d4 8spl2ygWuvX2eRF+xRlnVG5K8eXCXMB/dim6e04CoTYQcaysoNgEq1wWLlLGm4l+Ob8J PrCKEgiNP4uvgyf7nH/ww0FGeXmaC6vrPESW/E6FfsjTKj2RHLxxQA12KSiJua4pfTTo 7NNnGXOTxFIVhpr9ZpP6IZQounicHnTidX8l7qIHyS/JC36bjOgsW78Co+rUnIV1Z+zC AzKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="FvA+n/9w"; 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=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m13si41931edv.524.2021.08.16.12.50.42; Mon, 16 Aug 2021 12:51:06 -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=@redhat.com header.s=mimecast20190719 header.b="FvA+n/9w"; 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=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229802AbhHPTtk (ORCPT + 99 others); Mon, 16 Aug 2021 15:49:40 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:32846 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229587AbhHPTtj (ORCPT ); Mon, 16 Aug 2021 15:49:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1629143347; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=celAbCKZabJZZFRZffcKhJ6LMDssZFIlnWwC0cSAxUw=; b=FvA+n/9wDzYmGFmiFFB1W2PzcYjjOp1Zw3PYzwDaN3Z0B6BQonO5+HrAYfyb6fV1WINiI+ 56H43ZIzxKQgQ9GRmDKVNMfa6MJ2zQ7Irq1pJYLc3p5ZkeUDwFPnafn2ZohHTk04ZHWYqF GHFJK5bgmgKcIEv9IC90Q1unnOBI0xo= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-468-xgYfjmJTOHO8yHlRfEeJNw-1; Mon, 16 Aug 2021 15:49:06 -0400 X-MC-Unique: xgYfjmJTOHO8yHlRfEeJNw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4E4DD1082924; Mon, 16 Aug 2021 19:49:05 +0000 (UTC) Received: from t480s.redhat.com (unknown [10.39.192.85]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5F0865C1D5; Mon, 16 Aug 2021 19:48:41 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: David Hildenbrand , Linus Torvalds , Andrew Morton , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Alexander Viro , Alexey Dobriyan , Steven Rostedt , Peter Zijlstra , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Petr Mladek , Sergey Senozhatsky , Andy Shevchenko , Rasmus Villemoes , Kees Cook , "Eric W. Biederman" , Greg Ungerer , Geert Uytterhoeven , Mike Rapoport , Vlastimil Babka , Vincenzo Frascino , Chinwen Chang , Catalin Marinas , "Matthew Wilcox (Oracle)" , Huang Ying , Jann Horn , Feng Tang , Kevin Brodsky , Michael Ellerman , Shawn Anastasio , Steven Price , Nicholas Piggin , Christian Brauner , Jens Axboe , Gabriel Krisman Bertazi , Peter Xu , Suren Baghdasaryan , Shakeel Butt , Marco Elver , Daniel Jordan , Nicolas Viennot , Thomas Cedeno , Michal Hocko , Miklos Szeredi , Chengguang Xu , =?UTF-8?q?Christian=20K=C3=B6nig?= , Florian Weimer , David Laight , linux-unionfs@vger.kernel.org, linux-api@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 0/7] Remove in-tree usage of MAP_DENYWRITE Date: Mon, 16 Aug 2021 21:48:33 +0200 Message-Id: <20210816194840.42769-1-david@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This series removes all in-tree usage of MAP_DENYWRITE from the kernel and removes VM_DENYWRITE. We stopped supporting MAP_DENYWRITE for user space applications a while ago because of the chance for DoS. The last renaming user is binfmt binary loading during exec and legacy library loading via uselib(). With this change, MAP_DENYWRITE is effectively ignored throughout the kernel. Although the net change is small (well, we actually add code and comments), I think the cleanup in mmap() is quite nice. There are some (minor) user-visible changes with this series: 1. We no longer deny write access to shared libaries loaded via legacy uselib(); this behavior matches modern user space e.g., via dlopen(). 2. We no longer deny write access to the elf interpreter after exec completed, treating it just like shared libraries (which it often is). 3. We always deny write access to the file linked via /proc/pid/exe: sys_prctl(PR_SET_MM_MAP/EXE_FILE) will fail if write access to the file cannot be denied, and write access to the file will remain denied until the link is effectivel gone (exec, termination, sys_prctl(PR_SET_MM_MAP/EXE_FILE)) -- just as if exec'ing the file. There is a related problem [2] with overlayfs, that should at least partly be tackled by this series. I don't quite understand the interaction of overlayfs and deny_write_access()/allow_write_access() at exec time: If we end up denying write access to the wrong file and not to the realfile, that would be fundamentally broken. We would have to reroute our deny_write_access()/ allow_write_access() calls for the exec file to the realfile -- but I leave figuring out the details to overlayfs guys, as that would be a related but different issue. There was a lengthy discussion in [3] whether to remove deny_write_access() completely; however, if we decide to go that way, it would ideally be done on top, because it could be that some applications even rely on the current behavior. v1 -> v2: - "kernel/fork: factor out replacing the current MM exe_file" -- Call the function "replace_mm_exe_file()" instead -- Add some doc, similar to set_mm_exe_file() -- Update patch subject/description - "kernel/fork: always deny write access to current MM exe_file" -- Introduce dup_mm_exe_file() -- Make set_mm_exe_file() return an error to make the code easier to grasp. -- Improve comments - Added ACKs - Mention "sys_prctl(PR_SET_MM_MAP/EXE_FILE)" everywhere instead of only "sys_prctl(PR_SET_MM_EXE_FILE)". RFC -> v1: - "binfmt: remove in-tree usage of MAP_DENYWRITE" -- Add a note that this should fix part of a problem with overlayfs [1] https://lore.kernel.org/r/20210423131640.20080-1-david@redhat.com/ [2] https://lore.kernel.org/r/YNHXzBgzRrZu1MrD@miu.piliscsaba.redhat.com/ [3] https://lkml.kernel.org/r/20210812084348.6521-1-david@redhat.com Cc: Linus Torvalds Cc: Andrew Morton Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: "H. Peter Anvin" Cc: Alexander Viro Cc: Alexey Dobriyan Cc: Steven Rostedt Cc: Peter Zijlstra Cc: Arnaldo Carvalho de Melo Cc: Mark Rutland Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Namhyung Kim Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Andy Shevchenko Cc: Rasmus Villemoes Cc: Kees Cook Cc: "Eric W. Biederman" Cc: Greg Ungerer Cc: Geert Uytterhoeven Cc: Mike Rapoport Cc: Vlastimil Babka Cc: Vincenzo Frascino Cc: Chinwen Chang Cc: Catalin Marinas Cc: "Matthew Wilcox (Oracle)" Cc: Huang Ying Cc: Jann Horn Cc: Feng Tang Cc: Kevin Brodsky Cc: Michael Ellerman Cc: Shawn Anastasio Cc: Steven Price Cc: Nicholas Piggin Cc: Christian Brauner Cc: Jens Axboe Cc: Gabriel Krisman Bertazi Cc: Peter Xu Cc: Suren Baghdasaryan Cc: Shakeel Butt Cc: Marco Elver Cc: Daniel Jordan Cc: Nicolas Viennot Cc: Thomas Cedeno Cc: Michal Hocko Cc: Miklos Szeredi Cc: Chengguang Xu Cc: "Christian König" Cc: Florian Weimer Cc: Al Viro Cc: David Laight Cc: linux-unionfs@vger.kernel.org Cc: linux-api@vger.kernel.org Cc: x86@kernel.org Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org David Hildenbrand (7): binfmt: don't use MAP_DENYWRITE when loading shared libraries via uselib() kernel/fork: factor out replacing the current MM exe_file kernel/fork: always deny write access to current MM exe_file binfmt: remove in-tree usage of MAP_DENYWRITE mm: remove VM_DENYWRITE mm: ignore MAP_DENYWRITE in ksys_mmap_pgoff() fs: update documentation of get_write_access() and friends arch/x86/ia32/ia32_aout.c | 8 ++- fs/binfmt_aout.c | 7 ++- fs/binfmt_elf.c | 6 +-- fs/binfmt_elf_fdpic.c | 2 +- fs/exec.c | 4 +- fs/proc/task_mmu.c | 1 - include/linux/fs.h | 19 ++++--- include/linux/mm.h | 4 +- include/linux/mman.h | 4 +- include/trace/events/mmflags.h | 1 - kernel/events/core.c | 2 - kernel/fork.c | 95 ++++++++++++++++++++++++++++++---- kernel/sys.c | 33 +----------- lib/test_printf.c | 5 +- mm/mmap.c | 29 ++--------- mm/nommu.c | 2 - 16 files changed, 119 insertions(+), 103 deletions(-) base-commit: 7c60610d476766e128cc4284bb6349732cbd6606 -- 2.31.1