Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp1061131imm; Wed, 18 Jul 2018 16:01:05 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcK8y5ZSgj9nLn4y8OOZIok7OPPl8F/GTfv86y3Qqc0aK+EUgzZ13UtShh36SKnXPAvYeyJ X-Received: by 2002:a62:8d84:: with SMTP id p4-v6mr7127834pfk.251.1531954865919; Wed, 18 Jul 2018 16:01:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531954865; cv=none; d=google.com; s=arc-20160816; b=EJUgUufGgdrLMT5kCF7bgOX+hrB0Vq3fTh3a0e0/9pqk7CH13QipmVtaYEoXxnuc4R OgK3CT4V4uBDn4QGCouyz7wAj0DVgXzKHiIsqMSuSjUuVnB3vRsnP/I8ccpG+W4bfyHx HyaaV/2Bw7KSf+uh/0ACLjU/kzEcyf0Slz1CM4WLZqfAFFL8nrCZ0kbr4h3PuzJKcC7K Z6FjnV2g6P4aG9N6pIdQNVyv6yPGKkxCrxWlug3Lf6DKWkMbavdPw1ixB4aNO8OVAxSb /WIJinU87YmJBfL8y1jzng1FzE8yjUO29VPKZvwnqHf31B1cVeuVrcxfA5M2MYVW6ZR1 UtLw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=pZjRvZxPQspBLpfeaPSiIUV9ZEirc8l71XrDkFv8j6M=; b=Npi56ApoPZcnQ6lDPlys/45ZJsQKPlYLrqRMA21vF6lDovIXEKFSgrQUtmNGxW5fCc 8ZtTa/t6W9nW1qSlQDi8ZLEHFE+UIzrcmN64o8WthDfBGA2xWTbRF6c/IRrHOfENtfdK lpJTC475BwKb6bb/2d3vK9J5Pj3iwglRpaZa/hb5si3JO07+gVddl8SBb4EtaEmYrBIy ISB87xL8uEtq6TPbF0viTm8/Sebvon2uEcBfkVWfC2qNTF5Ci2bnqHHIkCjenkCJDHG2 kL+nMgY0pMVO5Nm9emQg2Ff32nsO6x/uOcPwaj6v1GGmsPgjYml/Iuv3WFWiEN5s5MpL yJZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=bJTzg4wN; 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 r128-v6si4155198pgr.634.2018.07.18.16.00.51; Wed, 18 Jul 2018 16:01:05 -0700 (PDT) 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=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=bJTzg4wN; 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 S1730906AbeGRXj6 (ORCPT + 99 others); Wed, 18 Jul 2018 19:39:58 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:34073 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730382AbeGRXj0 (ORCPT ); Wed, 18 Jul 2018 19:39:26 -0400 Received: by mail-pg1-f193.google.com with SMTP id y5-v6so2671823pgv.1 for ; Wed, 18 Jul 2018 15:59:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=pZjRvZxPQspBLpfeaPSiIUV9ZEirc8l71XrDkFv8j6M=; b=bJTzg4wNOorGyL7+qi9C5rAmoFuUTvCyWVvOEm2x4lgXNH/XtGZ8UNyPyVrAPkIOJN nBZ/JH5SRJp3F/4acrWKWr6YphpXHHBP54dxjc8CXfkX/nRXxHW+/i4UqpM4BXIU80Rz gyONi92GoFaRVWIEofvbegEVRApOdX87xxAmiOhXOjT4xItWoiTmUUv77+VBN9aaO3um hL3AgddScjj6kUCLqw6192mxdoj1o9qxlG9xB/64+QUCvNDbPJPE7/+NqZeHYhcADpNz wFzY/Mit/dKnUXYc7C4pyEhcZSKodt9GDSGGaUNX180jGeKPuzuqCAJgL+x3W3w56Dea ZVWQ== 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:in-reply-to :references; bh=pZjRvZxPQspBLpfeaPSiIUV9ZEirc8l71XrDkFv8j6M=; b=rMJB4Hv0kUC2WgXv/Z+KLfc9m96y4BIgyI9GsYVS4/w+gMIGbf/qCeGBrYEDprPu/s itUi2gqv5H8Un1DTCeYvlTYrYPBo3D7Y1joSfzttEpL59HcJq0ZegYhK/9fXBC1xarDX kxoPay6Eg6Oi/TGZCMQxrUJ6pQ3+OW9+AjQAeaOOjNd1Zfabe6Guhfp/Q4oyMebTgRCp kRqetRmv0w1Szw+8Npop1FEj7XHAI3ZTEfYw6r/G/nkCADS9L29fnOet9qPn+kENBS3h CRSOKuPxjayPmJpW4lqjraequ6+ByOpui02zqxBJYuOs75lS7Idw3TrEHVEr15ol+DOS JPmg== X-Gm-Message-State: AOUpUlFCpVAdjG6IhpLz3LOW4N2iwiMBSQvsEf4ngqu99tnhFVcW6X7S Ezddqvk7ZTl8tcxnYwIJJs7aC8LhPMw= X-Received: by 2002:a63:ff21:: with SMTP id k33-v6mr7381054pgi.38.1531954758650; Wed, 18 Jul 2018 15:59:18 -0700 (PDT) Received: from vader.hsd1.wa.comcast.net ([2601:602:8800:a9a9:e6a7:a0ff:fe0b:c9a8]) by smtp.gmail.com with ESMTPSA id s16-v6sm6377946pfm.114.2018.07.18.15.59.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 18 Jul 2018 15:59:18 -0700 (PDT) From: Omar Sandoval To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Andrew Morton Cc: Alexey Dobriyan , Eric Biederman , James Morse , Bhupesh Sharma , kernel-team@fb.com Subject: [PATCH v3 5/8] proc/kcore: hold lock during read Date: Wed, 18 Jul 2018 15:58:45 -0700 Message-Id: X-Mailer: git-send-email 2.18.0 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Omar Sandoval Now that we're using an rwsem, we can hold it during the entirety of read_kcore() and have a common return path. This is preparation for the next change. Signed-off-by: Omar Sandoval --- fs/proc/kcore.c | 70 ++++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/fs/proc/kcore.c b/fs/proc/kcore.c index 95aa988c5b5d..e317ac890871 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -440,19 +440,18 @@ static ssize_t read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) { char *buf = file->private_data; - ssize_t acc = 0; size_t size, tsz; size_t elf_buflen; int nphdr; unsigned long start; + size_t orig_buflen = buflen; + int ret = 0; down_read(&kclist_lock); size = get_kcore_size(&nphdr, &elf_buflen); - if (buflen == 0 || *fpos >= size) { - up_read(&kclist_lock); - return 0; - } + if (buflen == 0 || *fpos >= size) + goto out; /* trim buflen to not go beyond EOF */ if (buflen > size - *fpos) @@ -465,28 +464,26 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) tsz = elf_buflen - *fpos; if (buflen < tsz) tsz = buflen; - elf_buf = kzalloc(elf_buflen, GFP_ATOMIC); + elf_buf = kzalloc(elf_buflen, GFP_KERNEL); if (!elf_buf) { - up_read(&kclist_lock); - return -ENOMEM; + ret = -ENOMEM; + goto out; } elf_kcore_store_hdr(elf_buf, nphdr, elf_buflen); - up_read(&kclist_lock); if (copy_to_user(buffer, elf_buf + *fpos, tsz)) { kfree(elf_buf); - return -EFAULT; + ret = -EFAULT; + goto out; } kfree(elf_buf); buflen -= tsz; *fpos += tsz; buffer += tsz; - acc += tsz; /* leave now if filled buffer already */ if (buflen == 0) - return acc; - } else - up_read(&kclist_lock); + goto out; + } /* * Check to see if our file offset matches with any of @@ -499,25 +496,29 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) while (buflen) { struct kcore_list *m; - down_read(&kclist_lock); list_for_each_entry(m, &kclist_head, list) { if (start >= m->addr && start < (m->addr+m->size)) break; } - up_read(&kclist_lock); if (&m->list == &kclist_head) { - if (clear_user(buffer, tsz)) - return -EFAULT; + if (clear_user(buffer, tsz)) { + ret = -EFAULT; + goto out; + } } else if (m->type == KCORE_VMALLOC) { vread(buf, (char *)start, tsz); /* we have to zero-fill user buffer even if no read */ - if (copy_to_user(buffer, buf, tsz)) - return -EFAULT; + if (copy_to_user(buffer, buf, tsz)) { + ret = -EFAULT; + goto out; + } } else if (m->type == KCORE_USER) { /* User page is handled prior to normal kernel page: */ - if (copy_to_user(buffer, (char *)start, tsz)) - return -EFAULT; + if (copy_to_user(buffer, (char *)start, tsz)) { + ret = -EFAULT; + goto out; + } } else { if (kern_addr_valid(start)) { /* @@ -525,26 +526,35 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos) * hardened user copy kernel text checks. */ if (probe_kernel_read(buf, (void *) start, tsz)) { - if (clear_user(buffer, tsz)) - return -EFAULT; + if (clear_user(buffer, tsz)) { + ret = -EFAULT; + goto out; + } } else { - if (copy_to_user(buffer, buf, tsz)) - return -EFAULT; + if (copy_to_user(buffer, buf, tsz)) { + ret = -EFAULT; + goto out; + } } } else { - if (clear_user(buffer, tsz)) - return -EFAULT; + if (clear_user(buffer, tsz)) { + ret = -EFAULT; + goto out; + } } } buflen -= tsz; *fpos += tsz; buffer += tsz; - acc += tsz; start += tsz; tsz = (buflen > PAGE_SIZE ? PAGE_SIZE : buflen); } - return acc; +out: + up_write(&kclist_lock); + if (ret) + return ret; + return orig_buflen - buflen; } -- 2.18.0