Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp645628ybv; Wed, 19 Feb 2020 06:29:18 -0800 (PST) X-Google-Smtp-Source: APXvYqxpL6Jt2BdsGcceJrVjCHoIenz9GbmBFngoPwIpBghxiVlesMN6uqd9B9+ZTEDT7i8gEhvW X-Received: by 2002:a9d:5e82:: with SMTP id f2mr18995888otl.240.1582122558476; Wed, 19 Feb 2020 06:29:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582122558; cv=none; d=google.com; s=arc-20160816; b=S2vHimGxi65MfXtr9rUzK1DQToZXCLyjdDCRIqNUBm3JzBKnGw2CG1xfYaDyyR8xnZ 3N4EeOb/6ayaLxBx2JFsDBDkOVB2yMN97AwDmXc4QRahAEcC4WibEoolrueieDiPhbcH BhJnI91IlRsJlmBXcgiVg/9COAAXJOK8aJf+AKRjNdV8kVha7e8SaVbg4mz/SbJ/cvjy 581yqCncAFDS8ggmwRBhHo354RP5BCqueHGlaCQ2+MZzPlziHm8fL8+MXaUrT7uFt+1r 4zGtloMy6y2GhIwmiT2+KCawNp6DwMvDbaLUkSWNwPGILtCJ/yzcUpYFS+2JYXL6C8jT aN1w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=M/WOQ4dpD5ui5gn3HK5r3YR0sSK5JjTGZgiU8nuYjiY=; b=0QxGxVG0n61dGOjfHdwZ0i1uRpYeJG6zka85dlwXOYKIGqlPQJ3xqyqUJ/BjJnlyo2 DoIMbfvDoFOmR0HHFqogroPVVBSREJ8zD/2AZzHpljDgE4FNKmpsiJlLSiyt+oO95Aqa kwwn6NWNt2qvAGhyCwgAYPoMauVVfCr5a43N+nq08yTRbThGBO78fYlRi+9EOpb2a8pj iGC9rKfVFAUHKUMa1slP6C6ZhTII4+fjHGPzPm9a7Stud0SwC0IYskX4q43/Q5DTC19j eNQfG3eHuP0AHX3cBiTQpavn+75THcuc2dqz2AgAMPis8TfQHut30+R+H13wZA3IpBZq UUew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=OvI3GdWa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h21si1039328otr.235.2020.02.19.06.29.06; Wed, 19 Feb 2020 06:29:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=OvI3GdWa; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727951AbgBSO2x (ORCPT + 99 others); Wed, 19 Feb 2020 09:28:53 -0500 Received: from mail-qt1-f193.google.com ([209.85.160.193]:38531 "EHLO mail-qt1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726736AbgBSO2w (ORCPT ); Wed, 19 Feb 2020 09:28:52 -0500 Received: by mail-qt1-f193.google.com with SMTP id i23so305403qtr.5 for ; Wed, 19 Feb 2020 06:28:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=from:to:cc:subject:date:message-id; bh=M/WOQ4dpD5ui5gn3HK5r3YR0sSK5JjTGZgiU8nuYjiY=; b=OvI3GdWaULkHM6L7NQNGiL1tt+87WC0aV1sO7nxEF6vXn1H1/McHlo6NEux0NpiDAE eXuxxl8CwvjBJoNkxUgLeXRQ4aM8Oohu6ZPrDF3WYVmrlm/ogr5eim0ZUrec2+Q3qq/A 0szU0cwq2GMLj1y3zeR4BqAxDsbGXtzzp20Uc8506FOtPqSaBESxQc41R7gvT6ME+Yhh SPMHj4NjfddXYPjXpKc+9s39XhceWqbDYjFYErdd3LRFzIkYWqvfrokwF4C0kgYTXAjm 4tGwt6qg4BPCS9o+GOkTe8X1NPkW6IX5oZ6miDSXl4NnrrBCciCnMCUEwoTTj2T1nlR1 VrlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=M/WOQ4dpD5ui5gn3HK5r3YR0sSK5JjTGZgiU8nuYjiY=; b=Mtjqp0ZMXoCujedD9jUUo7TOAN+rD1imSLMBp7+1LHTsQWSpG/ZFBih30ZTUUvmDs4 slKSpAzPsqWK80Q35v5wCJTUIPDjo9abjSxW1vG6c92MOb/mbsYeTzfxU4KgEeY8Cbzk NtNQNudFGGp/EJSC7yciC5w3VpA6cHnxEHbhgvMYD7P4DWobojfVKcwhrnZAlPKnjpnu 4JDQ6hT9bGBZpGsO6jjx1hGqqYKhCajY0LYrcMIkyZ3nSf9/xgc4v7JhYS71Dejjf5dV a07lifHl7UOcD55nH45TN2hBSIzi2tEoMRUgNGWXszKSa7vzBFDNQi7mOzRNYYiEOvoH tgdQ== X-Gm-Message-State: APjAAAVfUEDaZeFLMGx+LgMFWKq0oBqK8piNRoGdEL7I0syilHyy3V5l zVRN457TJnzxom60kGQwxKQAyg== X-Received: by 2002:ac8:7765:: with SMTP id h5mr22389403qtu.223.1582122530154; Wed, 19 Feb 2020 06:28:50 -0800 (PST) Received: from qcai.nay.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id c26sm57012qtn.19.2020.02.19.06.28.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 19 Feb 2020 06:28:49 -0800 (PST) From: Qian Cai To: paulmck@kernel.org Cc: akpm@linux-foundation.org, kirill@shutemov.name, elver@google.com, peterz@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH -next v2] fork: annotate a data race in vm_area_dup() Date: Wed, 19 Feb 2020 09:28:15 -0500 Message-Id: <1582122495-12885-1-git-send-email-cai@lca.pw> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org struct vm_area_struct could be accessed concurrently as noticed by KCSAN, write to 0xffff9cf8bba08ad8 of 8 bytes by task 14263 on cpu 35: vma_interval_tree_insert+0x101/0x150: rb_insert_augmented_cached at include/linux/rbtree_augmented.h:58 (inlined by) vma_interval_tree_insert at mm/interval_tree.c:23 __vma_link_file+0x6e/0xe0 __vma_link_file at mm/mmap.c:629 vma_link+0xa2/0x120 mmap_region+0x753/0xb90 do_mmap+0x45c/0x710 vm_mmap_pgoff+0xc0/0x130 ksys_mmap_pgoff+0x1d1/0x300 __x64_sys_mmap+0x33/0x40 do_syscall_64+0x91/0xc44 entry_SYSCALL_64_after_hwframe+0x49/0xbe read to 0xffff9cf8bba08a80 of 200 bytes by task 14262 on cpu 122: vm_area_dup+0x6a/0xe0 vm_area_dup at kernel/fork.c:362 __split_vma+0x72/0x2a0 __split_vma at mm/mmap.c:2661 split_vma+0x5a/0x80 mprotect_fixup+0x368/0x3f0 do_mprotect_pkey+0x263/0x420 __x64_sys_mprotect+0x51/0x70 do_syscall_64+0x91/0xc44 entry_SYSCALL_64_after_hwframe+0x49/0xbe vm_area_dup() blindly copies all fields of original VMA to the new one. This includes coping vm_area_struct::shared.rb which is normally protected by i_mmap_lock. But this is fine because the read value will be overwritten on the following __vma_link_file() under proper protection. Thus, mark it as an intentional data race and insert a few assertions for the fields that should not be modified concurrently. Signed-off-by: Qian Cai --- v2: Insert a few assertions thanks to Marco. Update the commit log thanks to Kirill. kernel/fork.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/fork.c b/kernel/fork.c index cb2ae49e497e..67a5d691ffa8 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -359,7 +359,13 @@ struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); if (new) { - *new = *orig; + ASSERT_EXCLUSIVE_WRITER(orig->vm_flags); + ASSERT_EXCLUSIVE_WRITER(orig->vm_file); + /* + * orig->shared.rb may be modified concurrently, but the clone + * will be reinitialized. + */ + *new = data_race(*orig); INIT_LIST_HEAD(&new->anon_vma_chain); new->vm_next = new->vm_prev = NULL; } -- 1.8.3.1