Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp3742959pxb; Fri, 4 Feb 2022 15:39:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJzO7OSNCkL1NlKVaoTjW6WzHrIo5Dt90HeZBnfTzClCImfhUWh4inzOvJxHd2sS2o5bDmlt X-Received: by 2002:a17:903:1212:: with SMTP id l18mr5615091plh.45.1644017971094; Fri, 04 Feb 2022 15:39:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644017971; cv=none; d=google.com; s=arc-20160816; b=iTTYGPBcytx0srBxAhSASZ2H7LYdL7zBM7X7fstIEksAdbjO42k8JO2AuOvCWcRKL7 eJVz8og/Kgz+1Re13oPdSFYwziLP7OGFEFMIRD7PLiMLWj5fpxHEcz1V2QLPnQMMGcl8 VSOpdA0o1ZyziXcU4DmI+Wv7d6/I0qC+ytrneebnDAsZwuTCHUHa268jrECIuFEPI0A1 yXGBIHzCpv+iaaE0drLHwOUOL3OtBOVvlGJaTzkY1hrwIgNVmy8Z9S1wcRin8RC4deal fUYQJg1GNz2C64BdlLsEjO+y4f3YukJ0/BDuUB5YtFp2CioZTxBwuM6irbp9vCVP9pBS Z1MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=zCbdSgGi68nDz/yve/bgX706hkshLjqkzBpzirTtK9w=; b=x+DzGk+2Hddw3bXnOvBU/TpeiFacHVxlJmSkk6USxi/z/secR2Iia3Msd8kbhsCEzN lvvCf789odKKY9zrVSayQ0ln+8KeMRxBNK4QlbqzyPKO122Z6PPqhrrX469A0gpPtTad ygK00b8XrQfibVdhsVr32GEHpqoeYgRlab7I1cReo/hzRCtJq9xKDH2eHnju6G5LwCha CgZPSYoICp5+bvYmb4mvivxdRrubcUD7m0gqyl1bI3kkMrw9y1AHPn1U2YWo3viGQ76b HLjdnvYM2LG2FI76X4no+pkId1UjG6ohkSqU3X2OjlNilH9Dl0IE9WofgLLIhdk/N9Ge T4ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=MW124t1t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11si1602982plf.562.2022.02.04.15.39.17; Fri, 04 Feb 2022 15:39:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=MW124t1t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242142AbiBATDY (ORCPT + 99 others); Tue, 1 Feb 2022 14:03:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241038AbiBATDX (ORCPT ); Tue, 1 Feb 2022 14:03:23 -0500 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 13762C06173B for ; Tue, 1 Feb 2022 11:03:23 -0800 (PST) Received: by mail-lf1-x12b.google.com with SMTP id p27so35915971lfa.1 for ; Tue, 01 Feb 2022 11:03:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=zCbdSgGi68nDz/yve/bgX706hkshLjqkzBpzirTtK9w=; b=MW124t1ty/PYVAfbe0OIa6o8AMt7xGPQIsZZzlRt4hpIprOWPju7hLciOPYhTNQMOa Y+VDTi9VyjLfREYI5M97fsVW1HHR9wMcXAjgUJ8o0uNZfZGKRpAS6/BHfxCneaHgqrdS GCAcRBcqCQHjOJ8HpZTyGwXj3J8G7anBJcepOBQeVC/pAU5X2rEAZlM6sOL2Ukp6u5y4 FGBW5s8R1rEqOuuqtSTGPxILigghO5WtQosS1wS/TjazRF9rhmUDrKUKPfCQ2+gjLP+k gw1TOiBssrztSf0izbpMs9GI3I79UEK/jM2mZ0XW7Kfy09qEW/D8xH30NSQJsPOL5LY7 icug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=zCbdSgGi68nDz/yve/bgX706hkshLjqkzBpzirTtK9w=; b=zJhcc+DhAbD6UWlTWF7BCU+RCSE5wp7NyyeEPf9QZBEGacy9HynjQbDtqgmvqwD2/S jn4rvXdOSEkS9JjhlXlLU0ZGrrHIOItCwbrTEnRexYOBwgEPY7cdfFKZZmj8dqgdPwf5 WoINwPfYOeGTBpYvbP5dJXbdRG74mMf5hslaFBCfuab9nQER7sjPFIeyP7gmsm+gD9yo POTPlpzyLKmDRc+GbmEw/auAoMUze6LGkfYOHyPuFaIsfw5ROwzFk4uAw+FpwrskAIP7 OLodRpkxuvV44BHRN0kAjzNLqhKw/F8sNwAZ1iYp4eFQIUvn33fvSGNVS83nBQ5hKYup 0drA== X-Gm-Message-State: AOAM532Me4IULIOSwQOjV/+pwm/73iodegevp26mOxYihZQEgeS6vAur HpRsKi7KwUlZrbcNwK7AFzBBbPqpAg9FCj/YOgBevQ== X-Received: by 2002:a05:6512:441:: with SMTP id y1mr19860532lfk.315.1643742201155; Tue, 01 Feb 2022 11:03:21 -0800 (PST) MIME-Version: 1.0 References: <20220131153740.2396974-1-willy@infradead.org> <871r0nriy4.fsf@email.froward.int.ebiederm.org> <877dafq3bw.fsf@email.froward.int.ebiederm.org> <87bkzroica.fsf_-_@email.froward.int.ebiederm.org> <87iltzn3nd.fsf_-_@email.froward.int.ebiederm.org> In-Reply-To: <87iltzn3nd.fsf_-_@email.froward.int.ebiederm.org> From: Jann Horn Date: Tue, 1 Feb 2022 20:02:54 +0100 Message-ID: Subject: Re: [PATCH 5/5] coredump: Use the vma snapshot in fill_files_note To: "Eric W. Biederman" Cc: Matthew Wilcox , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Viro , Denys Vlasenko , Kees Cook , Vlastimil Babka , "Liam R . Howlett" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 31, 2022 at 7:47 PM Eric W. Biederman wrote: > Matthew Wilcox reported that there is a missing mmap_lock in > file_files_note that could possibly lead to a user after free. > > Solve this by using the existing vma snapshot for consistency > and to avoid the need to take the mmap_lock anywhere in the > coredump code except for dump_vma_snapshot. > > Update the dump_vma_snapshot to capture vm_pgoff and vm_file > that are neeeded by fill_files_note. > > Add free_vma_snapshot to free the captured values of vm_file. > > Reported-by: Matthew Wilcox > Link: https://lkml.kernel.org/r/20220131153740.2396974-1-willy@infradead.org > Cc: stable@vger.kernel.org > Fixes: a07279c9a8cd ("binfmt_elf, binfmt_elf_fdpic: use a VMA list snapshot") > Fixes: 2aa362c49c31 ("coredump: extend core dump note section to contain file names of mapped files") > Signed-off-by: "Eric W. Biederman" [...] > +static int fill_files_note(struct memelfnote *note, struct coredump_params *cprm) > { > struct mm_struct *mm = current->mm; > - struct vm_area_struct *vma; > unsigned count, size, names_ofs, remaining, n; > user_long_t *data; > user_long_t *start_end_ofs; > char *name_base, *name_curpos; > + int i; > > /* *Estimated* file count and total data size needed */ > count = mm->map_count; This function is still looking at mm->map_count in two spots, please change those spots to also look at cprm->vma_count. In particular the second one looks like it can cause memory corruption if the map_count changed since we created the snapshot. [...] > +static void free_vma_snapshot(struct coredump_params *cprm) > +{ > + if (cprm->vma_meta) { > + int i; > + for (i = 0; i < cprm->vma_count; i++) { > + struct file *file = cprm->vma_meta[i].file; > + if (file) > + fput(file); > + } > + kvfree(cprm->vma_meta); > + cprm->vma_meta = NULL; (this NULL write is superfluous, but it also doesn't hurt) > + } > +}