From patchwork Fri Oct 25 15:11:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13850920 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5BA20D0BB56 for ; Fri, 25 Oct 2024 15:12:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED8436B008A; Fri, 25 Oct 2024 11:12:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E87EA6B008C; Fri, 25 Oct 2024 11:12:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D4F666B0092; Fri, 25 Oct 2024 11:12:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B495F6B008A for ; Fri, 25 Oct 2024 11:12:20 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6C073120CAC for ; Fri, 25 Oct 2024 15:12:02 +0000 (UTC) X-FDA: 82712464884.06.DAF9F98 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 174894001E for ; Fri, 25 Oct 2024 15:11:50 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Rqj5EET6; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729868983; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RBGqG14Ir1TauPvJbSz3jQOA9+mQRgCc4BCTlJgnkuA=; b=M/cC88xADEA+SBW4hGkMsoRSJSiK7Vi0FsO6D/pOUuMbDuEEvC8Y+R/yVKdcqNHogpw1AB WOQtD3OrGxr3vlshZhZTBSgFdOlqY4r0tev9WAPz8Rplwdf93ayI6W9dz9fQdjw8JagFF5 rCWxtaykFF2NViXA/f8WfsZmP69nRWw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729868983; a=rsa-sha256; cv=none; b=8mChhsXGyAE6ACC6h+HzYzD27z2hzLNztBcY614hY1wLKydi3YyNv9Lan/6KoRIk8qdU/Q JhONgXtUproDeDml4PX4dY6M14ouSrYSRFwi+5uj2GbdbXYz9gukPVCRg0DlvwsYQUp/vA Jf+Ylx3pML5dNXC+O0JGY3MLDrRu8eA= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=Rqj5EET6; spf=pass (imf07.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729869137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RBGqG14Ir1TauPvJbSz3jQOA9+mQRgCc4BCTlJgnkuA=; b=Rqj5EET6GQwa0uXiipITubJYJzWfk20EteUrsW8ZAGEyP17keweeL/LyCahCAmY/ZfpakE Xj1vnyiAiGLhpWNrIJPw2Cra3uB5W0xLTltnwF26YHuEt5KfmcfneAbEA6EmCzffcAp/St AYqw8rJUf6ZS4U1sX4gszfJcHXMR3T0= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-602-pkIUU_7mPiOuJegdS3U3rg-1; Fri, 25 Oct 2024 11:12:13 -0400 X-MC-Unique: pkIUU_7mPiOuJegdS3U3rg-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7C5211955F43; Fri, 25 Oct 2024 15:12:10 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.22.65.27]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AF463300018D; Fri, 25 Oct 2024 15:12:02 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-s390@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org, linux-fsdevel@vger.kernel.org, kexec@lists.infradead.org, David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?utf-8?q?Eugenio_P=C3=A9rez?= , Baoquan He , Vivek Goyal , Dave Young , Thomas Huth , Cornelia Huck , Janosch Frank , Claudio Imbrenda , Eric Farman , Andrew Morton Subject: [PATCH v1 03/11] fs/proc/vmcore: disallow vmcore modifications after the vmcore was opened Date: Fri, 25 Oct 2024 17:11:25 +0200 Message-ID: <20241025151134.1275575-4-david@redhat.com> In-Reply-To: <20241025151134.1275575-1-david@redhat.com> References: <20241025151134.1275575-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 174894001E X-Stat-Signature: n5gfz4sexbtgxwi5t5wk3x3n7hw1w9kf X-HE-Tag: 1729869110-253758 X-HE-Meta: U2FsdGVkX1/lTiUj64rXjFXqkkdiKOBAxphNkLsjTM0RKmbuBhMHoKI9vPkKTPOMufEzRRFZAK9fFDSm0pudjcj11da9W4jVCEXzlRjwdC85NJOqLNw1vdduM2G6d/xYSJCQ71uOByKxftqdhmwganH1Z3hDmk5jr4i76UfzW/M20Caggvo9pqVuOzxcb8Tbbma2U5MnakBnH+yUH5H9N+0r5pIg5G+Ijo8f6s1b0Y/qf75WMVfxrhXi9MvjDEmCJFzXzRJaw5T8OK7KrGKuFJZEPU7SoxCtmfaCzoiCb0ZGpnrDUgZzFKr/n+zbzz5T0vD0jmqIdBZS+R83bHj9CBB6Vuj/s+IRLA0t/YgO5XvyRL2S424hnCB2IcNgYrinIJgJUS5DvoRb8zAVnMxfId80un73FQDd7y1juquhAfBY4Ssz/HsL2Tdcjfds0fMExjHfykMe7nnzAkz2rcdlTHzXFKVAPK/2gxv30L725wZlVYMxvdcWi9F/Bx7gShUCu3AlrDYNNBqrgvPgGx661G29KBGlaLrLKSSPwLOT0xhU5LXtA3xUCW/Sz4TVL2kihp94Q3Qxhz4Kl24ntuiG2qnPFVAIfDnu7AKI4M/goO2af7u+4Ozi3CYx/7/PEFa8nB9W+m+QvhGQ6hb7AsI7PhcvF8HaPwfObFmM0QqThabmS8PRpIzojea/V2mlzC7I2OqRhyk+QuHQP0eL5Gum1TdY6P2wgwBxxN02ZcXp2aOPqsm3rF4V/Fztoh1fjoVV1zReq2OTuRbLAdVrkfwJHOokw6WeSHpQp3ELo5VmjQ6QF7JOp0y98PS91o7tmpNk9tsMcC1gGMrvG5ndsPICIExk5gV3Fu57EUDs24p5IOFGMks1mQibyO2ts+7ofUboURhnQKafs8XYBwsMngtLSs/OPqMNTGriZdLk01f0Mq+Li49ga1b5TtWlpLTz40i+RnmPNgXZ5xhGXFvVCtT GcBqHJXi kgGIEK3Axlc8KpPD9UwdpkrxUAwMGu7hSUrAziTcdwJFJ57MEPjJ/MzoZE5uCV31ccRJbV40aLAF3E/WBMkjPmBrWF1kMGCLMiOtO3hOmSvmQKJYWN7DwJQC5g0sAJqTUoLhhHtyuDa/JeurTNPAYsaGxlflYmPZNOzgCUu77T/xLxB0Cf01rWt421olOdyncuVsnYP5ELcrLZQmC/6ysccbafd6zQzz5RmFoH7liuAWLtvQTGI7aNT7ukKCgaAg0yhAd5ZAu7hluoCZuD7s98zBthHcflaq0YnCn1Pwtm552Hn1clgMoLS0Wv/nNITTIWmFU5js2IuZYinU= 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: List-Subscribe: List-Unsubscribe: Let's protect all vmcore modifications by the vmcore_mutex and disallow vmcore modifications after the vmcore was opened: modifications would no longer be safe. Properly synchronize against concurrent opening of the vmcore. As a nice side-effect, we now properly protect concurrent vmcore modifications. No need to grab the mutex during mmap()/read(): after we opened the vmcore, modifications are impossible. Signed-off-by: David Hildenbrand --- fs/proc/vmcore.c | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/fs/proc/vmcore.c b/fs/proc/vmcore.c index b91c304463c9..6371dbaa21be 100644 --- a/fs/proc/vmcore.c +++ b/fs/proc/vmcore.c @@ -243,33 +243,27 @@ static int vmcoredd_copy_dumps(struct iov_iter *iter, u64 start, size_t size) { struct vmcoredd_node *dump; u64 offset = 0; - int ret = 0; size_t tsz; char *buf; - mutex_lock(&vmcore_mutex); list_for_each_entry(dump, &vmcoredd_list, list) { if (start < offset + dump->size) { tsz = min(offset + (u64)dump->size - start, (u64)size); buf = dump->buf + start - offset; - if (copy_to_iter(buf, tsz, iter) < tsz) { - ret = -EFAULT; - goto out_unlock; - } + if (copy_to_iter(buf, tsz, iter) < tsz) + return -EFAULT; size -= tsz; start += tsz; /* Leave now if buffer filled already */ if (!size) - goto out_unlock; + return 0; } offset += dump->size; } -out_unlock: - mutex_unlock(&vmcore_mutex); - return ret; + return 0; } #ifdef CONFIG_MMU @@ -278,20 +272,16 @@ static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst, { struct vmcoredd_node *dump; u64 offset = 0; - int ret = 0; size_t tsz; char *buf; - mutex_lock(&vmcore_mutex); list_for_each_entry(dump, &vmcoredd_list, list) { if (start < offset + dump->size) { tsz = min(offset + (u64)dump->size - start, (u64)size); buf = dump->buf + start - offset; if (remap_vmalloc_range_partial(vma, dst, buf, 0, - tsz)) { - ret = -EFAULT; - goto out_unlock; - } + tsz)) + return -EFAULT; size -= tsz; start += tsz; @@ -299,14 +289,12 @@ static int vmcoredd_mmap_dumps(struct vm_area_struct *vma, unsigned long dst, /* Leave now if buffer filled already */ if (!size) - goto out_unlock; + return 0; } offset += dump->size; } -out_unlock: - mutex_unlock(&vmcore_mutex); - return ret; + return 0; } #endif /* CONFIG_MMU */ #endif /* CONFIG_PROC_VMCORE_DEVICE_DUMP */ @@ -1482,6 +1470,10 @@ int vmcore_add_device_dump(struct vmcoredd_data *data) return -EINVAL; } + /* We'll recheck under lock later. */ + if (data_race(vmcore_opened)) + return -EBUSY; + if (!data || !strlen(data->dump_name) || !data->vmcoredd_callback || !data->size) return -EINVAL; @@ -1515,12 +1507,16 @@ int vmcore_add_device_dump(struct vmcoredd_data *data) dump->buf = buf; dump->size = data_size; - /* Add the dump to driver sysfs list */ + /* Add the dump to driver sysfs list and update the elfcore hdr */ mutex_lock(&vmcore_mutex); - list_add_tail(&dump->list, &vmcoredd_list); - mutex_unlock(&vmcore_mutex); + if (vmcore_opened) { + ret = -EBUSY; + goto out_err; + } + list_add_tail(&dump->list, &vmcoredd_list); vmcoredd_update_size(data_size); + mutex_unlock(&vmcore_mutex); return 0; out_err: