From patchwork Fri Jul 6 19:32:35 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10512439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 792D86024A for ; Fri, 6 Jul 2018 19:34:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 69763287AC for ; Fri, 6 Jul 2018 19:34:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5E1B6287B7; Fri, 6 Jul 2018 19:34:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F4171287B4 for ; Fri, 6 Jul 2018 19:34:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934854AbeGFTeB (ORCPT ); Fri, 6 Jul 2018 15:34:01 -0400 Received: from mail-pf0-f196.google.com ([209.85.192.196]:34555 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934742AbeGFTct (ORCPT ); Fri, 6 Jul 2018 15:32:49 -0400 Received: by mail-pf0-f196.google.com with SMTP id e10-v6so9187277pfn.1 for ; Fri, 06 Jul 2018 12:32:49 -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=/c2cbz49BSin2oba/l/28h6YQKrdT8yfUVnQW4QRAUA=; b=ZFCx5dflIcjiWh8ILZc2CbItNn/eY42n2GzU+mVzo8ZHcIGqQuOaGuByWbRQOFy2rX 8KExb8Di2as19gadUw0XL43sz72+lgRXrVCJXP5QqnY41Zr6jaWJL3/ykUAkUTMN7qdf Ym79DP//zBHEqlKCb2TD5m6uX6SvxrDuh6n/o3DLv4DopYeJ4O57HZimKck3s/LPeoR8 oDLkVZHeIIqazOSrFgSuC15rrT+POXOkGgb8oMSkVoN5fJfw1+A74czXewFJq6MA7oLO w54EnBHOmBHrTCvQFxzNbQmsgZHofBeHyHIAZMij3/SzOzSWy+lTF4fYNvq1De090E0F qLDg== 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=/c2cbz49BSin2oba/l/28h6YQKrdT8yfUVnQW4QRAUA=; b=g3P3v5cHTdkOD3lQtgKj28VQ+hIPclXKZgL4br9R9NuKyVxFOtL5XIYwqlD+Nwmxtb scl1Fxk7nHjusaCB5h7XkuXxasp5Ci265CsNRenPPiQ6ryrXkbcTdEYV/xFteb/3cyPE /XNFU/vBVaQ7xofa7NHcQMOjTA42y6tWl6ZtGaPa+HGAC96o16vjHvw5ruG9r+UyNZpm Wv/hbYfDf8ERvluwtWAKGJXPwP7FpNLeCQxuuZVHRj7SkdBTAiceZNRyH4tAQ8IaJNao O/rjsyyKyWdmpgAKq9t2Xi7o85WaslX//seACFLl41FMjDxoG6cqECU7YPUoY/4qauqO aq9g== X-Gm-Message-State: APt69E0vJY3GDqNSzlnrdrGlN60IEnE5NVRVUbGOijqbdOzJ4vzYhkDR FtmMxzx81peCqY+9DMJXkukXDw== X-Google-Smtp-Source: AAOMgpdAz8ENon+vSyTUOXTZIePNg/k4zf/VIx7Ind7wFokMvFcJBPOBZaaj70SHgohF0ETuU3a7gA== X-Received: by 2002:a62:9992:: with SMTP id t18-v6mr11809529pfk.239.1530905569241; Fri, 06 Jul 2018 12:32:49 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::4:984e]) by smtp.gmail.com with ESMTPSA id a11-v6sm12846162pgq.32.2018.07.06.12.32.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Jul 2018 12:32:48 -0700 (PDT) From: Omar Sandoval To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Alexey Dobriyan Cc: Eric Biederman , kernel-team@fb.com Subject: [PATCH 4/7] proc/kcore: hold lock during read Date: Fri, 6 Jul 2018 12:32:35 -0700 Message-Id: <974fd58d9eb5eb536a2b6b5580137ee567864c32.1530904769.git.osandov@fb.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 f335400300d3..b7ff2e2ec350 100644 --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -438,19 +438,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) @@ -463,28 +462,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 @@ -497,25 +494,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)) { /* @@ -523,26 +524,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; }