From patchwork Wed Apr 29 21:49:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jann Horn X-Patchwork-Id: 11518551 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 769CA1392 for ; Wed, 29 Apr 2020 21:50:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4332120B1F for ; Wed, 29 Apr 2020 21:50:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="TI7o13vZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4332120B1F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1347B8E0005; Wed, 29 Apr 2020 17:50:11 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0E5FB8E0001; Wed, 29 Apr 2020 17:50:11 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F16008E0005; Wed, 29 Apr 2020 17:50:10 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0154.hostedemail.com [216.40.44.154]) by kanga.kvack.org (Postfix) with ESMTP id D5DB08E0001 for ; Wed, 29 Apr 2020 17:50:10 -0400 (EDT) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 98174180AD815 for ; Wed, 29 Apr 2020 21:50:10 +0000 (UTC) X-FDA: 76762236180.11.boys85_17e61e3e325a X-Spam-Summary: 10,1,0,9972226971837ea9,d41d8cd98f00b204,3kfapxgukccih8llfemmejc.amkjglsv-kkit8ai.mpe@flex--jannh.bounces.google.com,,RULES_HIT:41:69:152:355:379:541:800:960:967:968:973:988:989:1260:1277:1313:1314:1345:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2393:2525:2560:2564:2682:2685:2859:2933:2937:2939:2942:2945:2947:2951:2954:3022:3138:3139:3140:3141:3142:3152:3354:3622:3865:3866:3867:3868:3870:3871:3872:3874:3934:3936:3938:3941:3944:3947:3950:3953:3956:3959:4117:4250:4321:4605:5007:6261:6653:6742:7875:7974:9010:9025:9969:10004:10400:10450:10455:11026:11658:11914:12043:12266:12291:12294:12296:12297:12438:12555:12663:12679:12683:12698:12737:12895:12986:13161:13229:13255:13618:13870:14096:14097:14157:14181:14394:14659:14721:19904:19999:21080:21325:21365:21444:21451:21524:21627:21740:21749:21795:21811:21939:30012:30051:30054:30056:30070:30079,0,RBL:209.85.160.202:@flex--jannh.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none ,Bayesia X-HE-Tag: boys85_17e61e3e325a X-Filterd-Recvd-Size: 6164 Received: from mail-qt1-f202.google.com (mail-qt1-f202.google.com [209.85.160.202]) by imf10.hostedemail.com (Postfix) with ESMTP for ; Wed, 29 Apr 2020 21:50:10 +0000 (UTC) Received: by mail-qt1-f202.google.com with SMTP id x7so4446207qtv.23 for ; Wed, 29 Apr 2020 14:50:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=P4HtBjpSOsceKegug2sEdVIRavAA18N3gAKqWZxS3Rg=; b=TI7o13vZ0R4PIgdk5y6o23r6CG8YctneHrjonN+9jVFDJ8a0023jr599F8e7C/pCP1 fL3VdXGkXbALChJNJ6t9m+IF3Wovo/x9/xrYX0g4YfMrgsQSVwwi23O/IaVGqNkQQ6h3 x+0qrgSjRfFwRKbeWgXwjoMlqjH1lm4wAM3CELbCKXTiLq/swJ34pmCu23AMfL5Yjkgy 2lr+IyuwfEsMfRYCu6HHrOrTyeTAAHhb+KjzIA+Iji3q7RLl2di8U0GeoekM5FEejqLO huXbxPxwrw4zSseLeMFqvnk/V1Q6Vuoaodwe0RRKcnawzra8PObdhok75bmLDdUPrs8e Ubdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=P4HtBjpSOsceKegug2sEdVIRavAA18N3gAKqWZxS3Rg=; b=iifXlLT70v9Nijp8VUVpp+/x6qlAukolcnns/awdvbVUNrzSGZ1mbb3tLXZYBX+jym WGXDiyIKysgVJ0p3ojumyTnOq35u/GgvYc2XwOYqH/wE6C7+63nEBGoS0QNrZ8i1bjAR BkvQAzvfcr69LYAIhX2dVksFn7oUoZTY49PIXNyGSj8tmJmaEa0b1uFPTFpEWHTTi99p VlCn4R6VdmtUTacLsl+s5bPsuIgttQw/zddyfhLsOv4JdE+2aYH3BKSqCXuk9LkZzm3P 8u3tUmzFYYWP6f42jWHlfo+EFxYgCRUUuho60oPfATfLmU5H1GKVHNktvuCWHuvdurOG fvXw== X-Gm-Message-State: AGi0PuZvFhdSSleZL4IZ10XU7x81iEzOfYyMZ2s5EgxJQDBIsUy1bi7c UHATBvlSftwG9D4bxoPXdX1G6U8OFw== X-Google-Smtp-Source: APiQypKMctOewJ3jBsbKHfX9HPIgYNkTG/naYrVaiwmZ+zyaqG0pLqe4doZmeqtJnRlWlz10BcfPBfAjMw== X-Received: by 2002:a0c:ba83:: with SMTP id x3mr36580637qvf.83.1588197009264; Wed, 29 Apr 2020 14:50:09 -0700 (PDT) Date: Wed, 29 Apr 2020 23:49:49 +0200 Message-Id: <20200429214954.44866-1-jannh@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.2.526.g744177e7f7-goog Subject: [PATCH v2 0/5] Fix ELF / FDPIC ELF core dumping, and use mmap_sem properly in there From: Jann Horn To: Andrew Morton Cc: Linus Torvalds , Christoph Hellwig , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Alexander Viro , "Eric W . Biederman" , Oleg Nesterov , Russell King , linux-arm-kernel@lists.infradead.org, Mark Salter , Aurelien Jacquiot , linux-c6x-dev@linux-c6x.org, Yoshinori Sato , Rich Felker , linux-sh@vger.kernel.org X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: At the moment, we have that rather ugly mmget_still_valid() helper to work around : ELF core dumping doesn't take the mmap_sem while traversing the task's VMAs, and if anything (like userfaultfd) then remotely messes with the VMA tree, fireworks ensue. So at the moment we use mmget_still_valid() to bail out in any writers that might be operating on a remote mm's VMAs. With this series, I'm trying to get rid of the need for that as cleanly as possible. In particular, I want to avoid holding the mmap_sem across unbounded sleeps. Patches 1, 2 and 3 are relatively unrelated cleanups in the core dumping code. Patches 4 and 5 implement the main change: Instead of repeatedly accessing the VMA list with sleeps in between, we snapshot it at the start with proper locking, and then later we just use our copy of the VMA list. This ensures that the kernel won't crash, that VMA metadata in the coredump is consistent even in the presence of concurrent modifications, and that any virtual addresses that aren't being concurrently modified have their contents show up in the core dump properly. The disadvantage of this approach is that we need a bit more memory during core dumping for storing metadata about all VMAs. After this series has landed, we should be able to rip out mmget_still_valid(). Testing done so far: - Creating a simple core dump on X86-64 still works. - The created coredump on X86-64 opens in GDB, and both the stack and the exectutable look vaguely plausible. - 32-bit ARM compiles with FDPIC support, both with MMU and !MMU config. I'm CCing some folks from the architectures that use FDPIC in case anyone wants to give this a spin. This series is based on (Christoph Hellwig's "remove set_fs calls from the coredump code v4"). changed in v2: - replace "Fix handling of partial writes in dump_emit()" with "Let dump_emit() bail out on short writes" (Linus) - get rid of the useless complicated cache flushing in "Take mmap_sem in get_dump_page()" (Linus) Jann Horn (5): binfmt_elf_fdpic: Stop using dump_emit() on user pointers on !MMU coredump: Let dump_emit() bail out on short writes coredump: Refactor page range dumping into common helper binfmt_elf, binfmt_elf_fdpic: Use a VMA list snapshot mm/gup: Take mmap_sem in get_dump_page() fs/binfmt_elf.c | 170 ++++++++++++--------------------------- fs/binfmt_elf_fdpic.c | 106 +++++++++--------------- fs/coredump.c | 123 +++++++++++++++++++++++++--- include/linux/coredump.h | 12 +++ mm/gup.c | 60 +++++++------- 5 files changed, 245 insertions(+), 226 deletions(-) base-commit: 6a8b55ed4056ea5559ebe4f6a4b247f627870d4c prerequisite-patch-id: c0a20b414eebc48fe0a8ca570b05de34c7980396 prerequisite-patch-id: 51973b8db0fa4b114e0c3fd8936b634d9d5061c5 prerequisite-patch-id: 0e1e8de282ca6d458dc6cbdc6b6ec5879edd8a05 prerequisite-patch-id: d5ee749c4d3a22ec80bd0dd88aadf89aeb569db8 prerequisite-patch-id: 46ce14e59e98e212a1eca0aef69c6dcdb62b8242