Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp193863imm; Thu, 12 Jul 2018 17:11:14 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcLGEm/YuqBLlVlVYfKJ19xWSQYqNlhSjF3Bsg835aNGedDARA18N7TzEcBRqBJEWClKdSW X-Received: by 2002:a62:da07:: with SMTP id c7-v6mr4551318pfh.106.1531440674906; Thu, 12 Jul 2018 17:11:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531440674; cv=none; d=google.com; s=arc-20160816; b=gaZIs20JahdM80hOsIhgb7m5IVd4qyLhudyVAjkfOIOM15CoqZhdEQam0jabF691Nf NyXZst30IJPtpWwQN51Zq9odVduXTJJzrjuLD8VW3qp6G4PxJqC/p+tydiMZXbb6abYk 2IeR5/1Gn9S/h3z7769oAuCSmvVn+ctTwj8Zj5WmSaJNVEyHxuM0u44SGSg54oPeaVVE ZtjjYXUyxSrzZ0eTCDiillYGzRph1sLuKVvdvua3PYAqXazn9oWVpJrAltYXRAMD16Ft xBHPxOP8zz1VDn5eSFwpYfniGrvJA6OmC1QPZfizjRQTKLQeebyHP1C3uCHJPUr6NrJx /OgQ== 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=002zvLp0GE64hLrexFk+R4hBFgfxdKVoabfUzR+Vo8Q=; b=QkhjAvIxhGCl9NFNm6sgIoeHBi9fTTeG/kLLsQOxoMgH51GxMyu1e4Bs8JteoB/0VB ijiAqitz/KbquE7hZPs+vv4MWA6l0kc8sm8G4i/eU9VxOtr/eHaL75mo7ytyXAGGk8gy gAuwo3+LKn/BhJ/+ND//CG6m/EbY/qargHJ+bEIn6z2UiUHj1SrE+UuMoo2uBZ+HJD7z FF/k91TJeyunyDp6Nm/SnwVwrToFja3PE/+JM5EBhXnjE5fjzgcghwyDGmf7tn+qY6/F NQTbk5TZcbfqkw7JAtaewTCp2h7iS1bFvPJQUfhDkw3BpSOMOprGVXLcQwE9sqQ9gVMS m4Vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@osandov-com.20150623.gappssmtp.com header.s=20150623 header.b=j1zJOCPP; 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 s9-v6si1059451plq.197.2018.07.12.17.11.00; Thu, 12 Jul 2018 17:11:14 -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=j1zJOCPP; 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 S2387880AbeGMAWJ (ORCPT + 99 others); Thu, 12 Jul 2018 20:22:09 -0400 Received: from mail-pl0-f65.google.com ([209.85.160.65]:35227 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387792AbeGMAVs (ORCPT ); Thu, 12 Jul 2018 20:21:48 -0400 Received: by mail-pl0-f65.google.com with SMTP id k1-v6so11357493plt.2 for ; Thu, 12 Jul 2018 17:09:50 -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=002zvLp0GE64hLrexFk+R4hBFgfxdKVoabfUzR+Vo8Q=; b=j1zJOCPPt1IADlHyGEMJA+5iVYJ8Itp7UkQ8IP4JDiQEC9D1+fZyyCuQ/fwJuYgRTK f8PgOMS3NBp7NMjKKn7Ag4Z9T3PjvGk5My8IxFv5LRlm/I+yAiiZvMbpyyak05J7aUcR tJTA9fAAbIEsWHU1pwhF60to+RVn6xghsMeObvqQP/qu3oB8h3q0d1RIiqIWwGIX7iJj e0wfg8ZpdrfAJpj5wO1RSpoecpJcKZ+nFhHHpFs+1xBBEanvXM541puJBQa+lBvvqgXu BSrphVkFJsrCQReNWUdyi7x2MwYZY4ek2KmygVSH6m25Q8dhIe1VNhJs/kanwmxmokC2 m79g== 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=002zvLp0GE64hLrexFk+R4hBFgfxdKVoabfUzR+Vo8Q=; b=sniuTU2MZxmHa/OTyg9MGH4bwZE74P4X+les7knUavJN1Id3TmJt0FMeS/nKW7+Ovp hcndNyRJ8Z5Aaduc3ONbvQWgUL8orYovGzrc4ZDKc2GZJZi3K4qMIciNFquYHpsL9j5F FYEfb/R24UDjxEeGJqF9MiQtcGYPEfhjKw+ZOFI8km7JJ0lsF8dGZCeAJXzZcOcNUdGp u1JJ7mY+ShL1NW1NqdfVsjyLymJbe4cvOXqZYh7LU957v1gsgTiSrCMyLuZaQt7CIUds b1bTxB1l26A91auIO1I20GORG2aupuQOgSndviJIFLoWP1u+xtyxfmJpgBjWR88E20CW 39tA== X-Gm-Message-State: AOUpUlERZsbRi2p3OTpBbtzNOGsrKQsbCe/ccYVCnZ3JxvjN1ndAeYSR +LvPKCSdQDvCJGYe4soRNjFW6hW6ibA= X-Received: by 2002:a17:902:8c88:: with SMTP id t8-v6mr4081820plo.117.1531440590147; Thu, 12 Jul 2018 17:09:50 -0700 (PDT) Received: from vader.thefacebook.com ([2620:10d:c090:200::5:74a0]) by smtp.gmail.com with ESMTPSA id b86-v6sm4452067pfj.35.2018.07.12.17.09.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 12 Jul 2018 17:09:49 -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 v2 4/7] proc/kcore: hold lock during read Date: Thu, 12 Jul 2018 17:09:36 -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 33667db6e370..f1ae848c7bcc 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