Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp80748ybv; Wed, 19 Feb 2020 16:49:23 -0800 (PST) X-Google-Smtp-Source: APXvYqx3Hmg7Jb+1OnbWBrOa++lFeaI7T19mu+cAk5PBIPjK8KJoNAgiDMQPO9kW0dmkgHF0LuHn X-Received: by 2002:aca:cf12:: with SMTP id f18mr277236oig.81.1582159763279; Wed, 19 Feb 2020 16:49:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582159763; cv=none; d=google.com; s=arc-20160816; b=KBgC9RiiV9mz+BD1vP0Dys6LoHCr/YgIg1KE9uoQaOay8nkeNciSlUs80+BM2zokSM vqcOOCnQ6ofw6+ZQhEn6zB8OiVkBUq586CvjXPY/fvZnupXB9kVQLMJTRKwHPZevI/IK i7NwznqlSDun4Y5AbxE9M0iuT9Dy37wQ8bf0e9QSjo4FlDIvdz1RHTqoVrAwqTfnx4+y pF+zALXm/oO6+C8GCYhMmeoD4CNHzKlZ3IkuWJeNrgRnk67uBEEGWpqO++I2k9IYgD8M 4022gXkjo29fyAhL93u9jZP7SOZ/OfFO7jiHWJGyan17XRDTMJI7ZM/yP3Ht3LnRj/ud 4iYA== 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; bh=PHpWXDTr6jV3C6iif6ncq7QaqfD+p0zDkr8t4LK8n2s=; b=CdPSmPzTiFDktp4YnvXQiTkVT1RO2MByqPoQJWH8LLW4tLJlWgujaW/f5I+Pl6I+yN 6TbezF5s95K1JfwJvHa1JQfBqq3YpSLVZb6aR48SVgQlgTVEdRCEKDEFf9IhstZ0PqmV iFbvcr4f4GNxOjUAo1t24jaYeVJpJSsXSoRAmP3pTvwJVs6Z6/WL/S1nR7IkFQfsrJZR XoliB8Oz3zJC5s5NhRX+hJMIY0nuTTdYRTHXKZUKtEBUHY5BdpQhoeLVdESWhtIyOhPd OzxrojGBy98zPy4SYlw0VsiAM3VuLb9a5pwDrf7+WnA5W1w2pzt0hZynregXoO4FnKgU 29ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@broadcom.com header.s=google header.b=UnfLCt7Z; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 107si797104oth.14.2020.02.19.16.49.00; Wed, 19 Feb 2020 16:49:23 -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=@broadcom.com header.s=google header.b=UnfLCt7Z; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=broadcom.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727506AbgBTAsp (ORCPT + 99 others); Wed, 19 Feb 2020 19:48:45 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:44992 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727469AbgBTAsn (ORCPT ); Wed, 19 Feb 2020 19:48:43 -0500 Received: by mail-pl1-f196.google.com with SMTP id d9so810717plo.11 for ; Wed, 19 Feb 2020 16:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PHpWXDTr6jV3C6iif6ncq7QaqfD+p0zDkr8t4LK8n2s=; b=UnfLCt7ZI/LJPiv9la+HUHVPOpCLsb/8lDB2QgzW6boVkAlLW+/1Ut8WR1iKRJNs8C NNQxZ58srqExB9/lyLJyw6dh2bf5K4xPkcaRAiZMiSSBm4f6NXAWtckrDh4WTd0rhT6l pE3zSEcxU0LMdxdTUfRoggIole3Vnwr9A+8iQ= 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=PHpWXDTr6jV3C6iif6ncq7QaqfD+p0zDkr8t4LK8n2s=; b=oR1tufEgHyodRKz5tEYxyvKhdWMjIurlnvpLoMMGz1jYvzI6K1/bClMV2SJ45fjdz7 Ru0APEbbqemnqXmcW4uvX3gyAZSqfLlz5IKKXgytj6HYTFa87xj+OWw4ibHO1cIDi++C /SzYzEWf5q+QjX1QWLsuVNh+Wciu5MFOSCTH9Uk7wK2PdhtxSK11rsFGntEy9C++THT8 ODnfE/UBhL+mfcnEwBqz5R6i1xiuI+bfRuM2YV6xjJ5NE0IrWGIZbL+61Xh6dM19cD5j YdjX5R6tG8xoD52jSbYnEUN/ac7QVtyR7aiEDViJ8KM5jKZPQTQlTpX2g9OIoDj7fSLQ GztA== X-Gm-Message-State: APjAAAUyI/YmuPVwN+e8Au5OriLrrpapXZURv4nMLNIJboCKSgxiWDG5 RXIA/XpxaJeJKsLxbJQpFwFYpQ== X-Received: by 2002:a17:902:bb83:: with SMTP id m3mr28970318pls.258.1582159722265; Wed, 19 Feb 2020 16:48:42 -0800 (PST) Received: from lbrmn-lnxub113.broadcom.net ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id 64sm816323pfd.48.2020.02.19.16.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2020 16:48:41 -0800 (PST) From: Scott Branden To: Luis Chamberlain , Greg Kroah-Hartman , David Brown , Alexander Viro , Shuah Khan , bjorn.andersson@linaro.org, Shuah Khan , Arnd Bergmann Cc: "Rafael J . Wysocki" , linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-fsdevel@vger.kernel.org, BCM Kernel Feedback , Olof Johansson , Andrew Morton , Dan Carpenter , Colin Ian King , Kees Cook , Takashi Iwai , linux-kselftest@vger.kernel.org, Andy Gross , Scott Branden Subject: [PATCH v2 1/7] fs: introduce kernel_pread_file* support Date: Wed, 19 Feb 2020 16:48:19 -0800 Message-Id: <20200220004825.23372-2-scott.branden@broadcom.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200220004825.23372-1-scott.branden@broadcom.com> References: <20200220004825.23372-1-scott.branden@broadcom.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add kernel_pread_file* support to kernel to allow for partial read of files with an offset into the file. Existing kernel_read_file functions call new kernel_pread_file functions with offset=0 and flags=KERNEL_PREAD_FLAG_WHOLE. Signed-off-by: Scott Branden --- fs/exec.c | 77 ++++++++++++++++++++++++++++++++++++---------- include/linux/fs.h | 15 +++++++++ 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index db17be51b112..a38f8fb432fa 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -896,10 +896,14 @@ struct file *open_exec(const char *name) } EXPORT_SYMBOL(open_exec); -int kernel_read_file(struct file *file, void **buf, loff_t *size, - loff_t max_size, enum kernel_read_file_id id) -{ - loff_t i_size, pos; +int kernel_pread_file(struct file *file, void **buf, loff_t *size, + loff_t pos, loff_t max_size, unsigned int flags, + enum kernel_read_file_id id) +{ + loff_t alloc_size; + loff_t buf_pos; + loff_t read_end; + loff_t i_size; ssize_t bytes = 0; int ret; @@ -919,21 +923,31 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, ret = -EINVAL; goto out; } - if (i_size > SIZE_MAX || (max_size > 0 && i_size > max_size)) { + + /* Default read to end of file */ + read_end = i_size; + + /* Allow reading partial portion of file */ + if ((flags & KERNEL_PREAD_FLAG_PART) && + (i_size > (pos + max_size))) + read_end = pos + max_size; + + alloc_size = read_end - pos; + if (i_size > SIZE_MAX || (max_size > 0 && alloc_size > max_size)) { ret = -EFBIG; goto out; } if (id != READING_FIRMWARE_PREALLOC_BUFFER) - *buf = vmalloc(i_size); + *buf = vmalloc(alloc_size); if (!*buf) { ret = -ENOMEM; goto out; } - pos = 0; - while (pos < i_size) { - bytes = kernel_read(file, *buf + pos, i_size - pos, &pos); + buf_pos = 0; + while (pos < read_end) { + bytes = kernel_read(file, *buf + buf_pos, read_end - pos, &pos); if (bytes < 0) { ret = bytes; goto out_free; @@ -941,14 +955,16 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, if (bytes == 0) break; + + buf_pos += bytes; } - if (pos != i_size) { + if (pos != read_end) { ret = -EIO; goto out_free; } - ret = security_kernel_post_read_file(file, *buf, i_size, id); + ret = security_kernel_post_read_file(file, *buf, alloc_size, id); if (!ret) *size = pos; @@ -964,10 +980,20 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size, allow_write_access(file); return ret; } +EXPORT_SYMBOL_GPL(kernel_pread_file); + +int kernel_read_file(struct file *file, void **buf, loff_t *size, + loff_t max_size, enum kernel_read_file_id id) +{ + return kernel_pread_file(file, buf, size, 0, max_size, + KERNEL_PREAD_FLAG_WHOLE, id); +} EXPORT_SYMBOL_GPL(kernel_read_file); -int kernel_read_file_from_path(const char *path, void **buf, loff_t *size, - loff_t max_size, enum kernel_read_file_id id) +int kernel_pread_file_from_path(const char *path, void **buf, + loff_t *size, loff_t pos, + loff_t max_size, unsigned int flags, + enum kernel_read_file_id id) { struct file *file; int ret; @@ -979,14 +1005,23 @@ int kernel_read_file_from_path(const char *path, void **buf, loff_t *size, if (IS_ERR(file)) return PTR_ERR(file); - ret = kernel_read_file(file, buf, size, max_size, id); + ret = kernel_pread_file(file, buf, size, pos, max_size, flags, id); fput(file); return ret; } +EXPORT_SYMBOL_GPL(kernel_pread_file_from_path); + +int kernel_read_file_from_path(const char *path, void **buf, loff_t *size, + loff_t max_size, enum kernel_read_file_id id) +{ + return kernel_pread_file_from_path(path, buf, size, 0, max_size, + KERNEL_PREAD_FLAG_WHOLE, id); +} EXPORT_SYMBOL_GPL(kernel_read_file_from_path); -int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, - enum kernel_read_file_id id) +int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size, loff_t pos, + loff_t max_size, unsigned int flags, + enum kernel_read_file_id id) { struct fd f = fdget(fd); int ret = -EBADF; @@ -994,11 +1029,19 @@ int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, if (!f.file) goto out; - ret = kernel_read_file(f.file, buf, size, max_size, id); + ret = kernel_pread_file(f.file, buf, size, pos, max_size, flags, id); out: fdput(f); return ret; } +EXPORT_SYMBOL_GPL(kernel_pread_file_from_fd); + +int kernel_read_file_from_fd(int fd, void **buf, loff_t *size, loff_t max_size, + enum kernel_read_file_id id) +{ + return kernel_pread_file_from_fd(fd, buf, size, 0, max_size, + KERNEL_PREAD_FLAG_WHOLE, id); +} EXPORT_SYMBOL_GPL(kernel_read_file_from_fd); ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len) diff --git a/include/linux/fs.h b/include/linux/fs.h index 3cd4fe6b845e..8f39530cfcc2 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3008,10 +3008,25 @@ static inline const char *kernel_read_file_id_str(enum kernel_read_file_id id) return kernel_read_file_str[id]; } +/* Flags used by kernel_pread_file functions */ +#define KERNEL_PREAD_FLAG_WHOLE 0x0000 /* Only Allow reading of whole file */ +#define KERNEL_PREAD_FLAG_PART 0x0001 /* Allow reading part of file */ + +extern int kernel_pread_file(struct file *file, void **buf, loff_t *size, + loff_t pos, loff_t max_size, unsigned int flags, + enum kernel_read_file_id id); extern int kernel_read_file(struct file *, void **, loff_t *, loff_t, enum kernel_read_file_id); +extern int kernel_pread_file_from_path(const char *path, void **buf, + loff_t *size, loff_t pos, + loff_t max_size, unsigned int flags, + enum kernel_read_file_id id); extern int kernel_read_file_from_path(const char *, void **, loff_t *, loff_t, enum kernel_read_file_id); +extern int kernel_pread_file_from_fd(int fd, void **buf, loff_t *size, + loff_t pos, loff_t max_size, + unsigned int flags, + enum kernel_read_file_id id); extern int kernel_read_file_from_fd(int, void **, loff_t *, loff_t, enum kernel_read_file_id); extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *); -- 2.17.1