All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Allow to kexec with initramfs larger than 2G
@ 2022-05-27  2:55 ` Pasha Tatashin
  0 siblings, 0 replies; 21+ messages in thread
From: Pasha Tatashin @ 2022-05-27  2:55 UTC (permalink / raw)
  To: pasha.tatashin, sashal, ebiederm, rburanyi, gthelen, viro, kexec,
	linux-fsdevel, linux-kernel

Currently, the largest initramfs that is supported by kexec_file_load()
syscall is 2G.

This is because kernel_read_file() returns int, and is limited to
INT_MAX or 2G.

On the other hand, there are kexec based boot loaders (i.e. u-root),
that may need to boot netboot images that might be larger than 2G.

The first patch changes the return type from int to ssize_t in
kernel_read_file* functions.

The second patch increases the maximum initramfs file size to 4G.

Tested: verified that can kexec_file_load() works with 4G initramfs
on x86_64.

Pasha Tatashin (2):
  fs/kernel_read_file: Allow to read files up-to ssize_t
  kexec_file: Increase maximum file size to 4G

 fs/kernel_read_file.c            | 38 ++++++++++++++++----------------
 include/linux/kernel_read_file.h | 32 +++++++++++++--------------
 include/linux/limits.h           |  1 +
 kernel/kexec_file.c              | 10 ++++++---
 4 files changed, 43 insertions(+), 38 deletions(-)

-- 
2.36.1.124.g0e6072fb45-goog


^ permalink raw reply	[flat|nested] 21+ messages in thread
* Re: [PATCH v2 1/2] fs/kernel_read_file: Allow to read files up-to ssize_t
@ 2022-05-28  3:06 kernel test robot
  0 siblings, 0 replies; 21+ messages in thread
From: kernel test robot @ 2022-05-28  3:06 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 10339 bytes --]

CC: kbuild-all(a)lists.01.org
BCC: lkp(a)intel.com
In-Reply-To: <20220527025535.3953665-2-pasha.tatashin@soleen.com>
References: <20220527025535.3953665-2-pasha.tatashin@soleen.com>
TO: Pasha Tatashin <pasha.tatashin@soleen.com>
TO: pasha.tatashin(a)soleen.com
TO: sashal(a)kernel.org
TO: ebiederm(a)xmission.com
TO: rburanyi(a)google.com
TO: gthelen(a)google.com
TO: viro(a)zeniv.linux.org.uk
TO: kexec(a)lists.infradead.org
TO: linux-fsdevel(a)vger.kernel.org
TO: linux-kernel(a)vger.kernel.org

Hi Pasha,

I love your patch! Perhaps something to improve:

[auto build test WARNING on linus/master]
[also build test WARNING on v5.18 next-20220527]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/intel-lab-lkp/linux/commits/Pasha-Tatashin/Allow-to-kexec-with-initramfs-larger-than-2G/20220527-105815
base:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 7f50d4dfe816dd916a7cbf39039674825c2b388b
:::::: branch date: 24 hours ago
:::::: commit date: 24 hours ago
config: x86_64-randconfig-m001 (https://download.01.org/0day-ci/archive/20220528/202205281006.650Vdx6y-lkp(a)intel.com/config)
compiler: gcc-11 (Debian 11.3.0-1) 11.3.0

If you fix the issue, kindly add following tag where applicable
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
fs/kernel_read_file.c:61 kernel_read_file() warn: impossible condition '(i_size > (((~0) >> 1))) => (s64min-s64max > s64max)'

vim +61 fs/kernel_read_file.c

5287b07f6d7cc3 Kees Cook      2020-10-02    7  
113eeb517780ad Kees Cook      2020-10-02    8  /**
113eeb517780ad Kees Cook      2020-10-02    9   * kernel_read_file() - read file contents into a kernel buffer
113eeb517780ad Kees Cook      2020-10-02   10   *
113eeb517780ad Kees Cook      2020-10-02   11   * @file	file to read from
0fa8e084648779 Kees Cook      2020-10-02   12   * @offset	where to start reading from (see below).
113eeb517780ad Kees Cook      2020-10-02   13   * @buf		pointer to a "void *" buffer for reading into (if
113eeb517780ad Kees Cook      2020-10-02   14   *		*@buf is NULL, a buffer will be allocated, and
113eeb517780ad Kees Cook      2020-10-02   15   *		@buf_size will be ignored)
113eeb517780ad Kees Cook      2020-10-02   16   * @buf_size	size of buf, if already allocated. If @buf not
113eeb517780ad Kees Cook      2020-10-02   17   *		allocated, this is the largest size to allocate.
885352881f11f1 Kees Cook      2020-10-02   18   * @file_size	if non-NULL, the full size of @file will be
885352881f11f1 Kees Cook      2020-10-02   19   *		written here.
113eeb517780ad Kees Cook      2020-10-02   20   * @id		the kernel_read_file_id identifying the type of
113eeb517780ad Kees Cook      2020-10-02   21   *		file contents being read (for LSMs to examine)
113eeb517780ad Kees Cook      2020-10-02   22   *
0fa8e084648779 Kees Cook      2020-10-02   23   * @offset must be 0 unless both @buf and @file_size are non-NULL
0fa8e084648779 Kees Cook      2020-10-02   24   * (i.e. the caller must be expecting to read partial file contents
0fa8e084648779 Kees Cook      2020-10-02   25   * via an already-allocated @buf, in at most @buf_size chunks, and
0fa8e084648779 Kees Cook      2020-10-02   26   * will be able to determine when the entire file was read by
0fa8e084648779 Kees Cook      2020-10-02   27   * checking @file_size). This isn't a recommended way to read a
0fa8e084648779 Kees Cook      2020-10-02   28   * file, though, since it is possible that the contents might
0fa8e084648779 Kees Cook      2020-10-02   29   * change between calls to kernel_read_file().
0fa8e084648779 Kees Cook      2020-10-02   30   *
113eeb517780ad Kees Cook      2020-10-02   31   * Returns number of bytes read (no single read will be bigger
71c48c48184de1 Pasha Tatashin 2022-05-27   32   * than SSIZE_MAX), or negative on error.
113eeb517780ad Kees Cook      2020-10-02   33   *
113eeb517780ad Kees Cook      2020-10-02   34   */
71c48c48184de1 Pasha Tatashin 2022-05-27   35  ssize_t kernel_read_file(struct file *file, loff_t offset, void **buf,
885352881f11f1 Kees Cook      2020-10-02   36  			 size_t buf_size, size_t *file_size,
885352881f11f1 Kees Cook      2020-10-02   37  			 enum kernel_read_file_id id)
5287b07f6d7cc3 Kees Cook      2020-10-02   38  {
5287b07f6d7cc3 Kees Cook      2020-10-02   39  	loff_t i_size, pos;
71c48c48184de1 Pasha Tatashin 2022-05-27   40  	ssize_t copied;
5287b07f6d7cc3 Kees Cook      2020-10-02   41  	void *allocated = NULL;
0fa8e084648779 Kees Cook      2020-10-02   42  	bool whole_file;
5287b07f6d7cc3 Kees Cook      2020-10-02   43  	int ret;
5287b07f6d7cc3 Kees Cook      2020-10-02   44  
0fa8e084648779 Kees Cook      2020-10-02   45  	if (offset != 0 && (!*buf || !file_size))
0fa8e084648779 Kees Cook      2020-10-02   46  		return -EINVAL;
0fa8e084648779 Kees Cook      2020-10-02   47  
113eeb517780ad Kees Cook      2020-10-02   48  	if (!S_ISREG(file_inode(file)->i_mode))
5287b07f6d7cc3 Kees Cook      2020-10-02   49  		return -EINVAL;
5287b07f6d7cc3 Kees Cook      2020-10-02   50  
5287b07f6d7cc3 Kees Cook      2020-10-02   51  	ret = deny_write_access(file);
5287b07f6d7cc3 Kees Cook      2020-10-02   52  	if (ret)
5287b07f6d7cc3 Kees Cook      2020-10-02   53  		return ret;
5287b07f6d7cc3 Kees Cook      2020-10-02   54  
5287b07f6d7cc3 Kees Cook      2020-10-02   55  	i_size = i_size_read(file_inode(file));
5287b07f6d7cc3 Kees Cook      2020-10-02   56  	if (i_size <= 0) {
5287b07f6d7cc3 Kees Cook      2020-10-02   57  		ret = -EINVAL;
5287b07f6d7cc3 Kees Cook      2020-10-02   58  		goto out;
5287b07f6d7cc3 Kees Cook      2020-10-02   59  	}
0fa8e084648779 Kees Cook      2020-10-02   60  	/* The file is too big for sane activities. */
71c48c48184de1 Pasha Tatashin 2022-05-27  @61  	if (i_size > SSIZE_MAX) {
5287b07f6d7cc3 Kees Cook      2020-10-02   62  		ret = -EFBIG;
5287b07f6d7cc3 Kees Cook      2020-10-02   63  		goto out;
5287b07f6d7cc3 Kees Cook      2020-10-02   64  	}
0fa8e084648779 Kees Cook      2020-10-02   65  	/* The entire file cannot be read in one buffer. */
0fa8e084648779 Kees Cook      2020-10-02   66  	if (!file_size && offset == 0 && i_size > buf_size) {
0fa8e084648779 Kees Cook      2020-10-02   67  		ret = -EFBIG;
0fa8e084648779 Kees Cook      2020-10-02   68  		goto out;
0fa8e084648779 Kees Cook      2020-10-02   69  	}
0fa8e084648779 Kees Cook      2020-10-02   70  
0fa8e084648779 Kees Cook      2020-10-02   71  	whole_file = (offset == 0 && i_size <= buf_size);
0fa8e084648779 Kees Cook      2020-10-02   72  	ret = security_kernel_read_file(file, id, whole_file);
0fa8e084648779 Kees Cook      2020-10-02   73  	if (ret)
0fa8e084648779 Kees Cook      2020-10-02   74  		goto out;
0fa8e084648779 Kees Cook      2020-10-02   75  
885352881f11f1 Kees Cook      2020-10-02   76  	if (file_size)
885352881f11f1 Kees Cook      2020-10-02   77  		*file_size = i_size;
5287b07f6d7cc3 Kees Cook      2020-10-02   78  
5287b07f6d7cc3 Kees Cook      2020-10-02   79  	if (!*buf)
5287b07f6d7cc3 Kees Cook      2020-10-02   80  		*buf = allocated = vmalloc(i_size);
5287b07f6d7cc3 Kees Cook      2020-10-02   81  	if (!*buf) {
5287b07f6d7cc3 Kees Cook      2020-10-02   82  		ret = -ENOMEM;
5287b07f6d7cc3 Kees Cook      2020-10-02   83  		goto out;
5287b07f6d7cc3 Kees Cook      2020-10-02   84  	}
5287b07f6d7cc3 Kees Cook      2020-10-02   85  
0fa8e084648779 Kees Cook      2020-10-02   86  	pos = offset;
0fa8e084648779 Kees Cook      2020-10-02   87  	copied = 0;
0fa8e084648779 Kees Cook      2020-10-02   88  	while (copied < buf_size) {
0fa8e084648779 Kees Cook      2020-10-02   89  		ssize_t bytes;
0fa8e084648779 Kees Cook      2020-10-02   90  		size_t wanted = min_t(size_t, buf_size - copied,
0fa8e084648779 Kees Cook      2020-10-02   91  					      i_size - pos);
0fa8e084648779 Kees Cook      2020-10-02   92  
0fa8e084648779 Kees Cook      2020-10-02   93  		bytes = kernel_read(file, *buf + copied, wanted, &pos);
5287b07f6d7cc3 Kees Cook      2020-10-02   94  		if (bytes < 0) {
5287b07f6d7cc3 Kees Cook      2020-10-02   95  			ret = bytes;
5287b07f6d7cc3 Kees Cook      2020-10-02   96  			goto out_free;
5287b07f6d7cc3 Kees Cook      2020-10-02   97  		}
5287b07f6d7cc3 Kees Cook      2020-10-02   98  
5287b07f6d7cc3 Kees Cook      2020-10-02   99  		if (bytes == 0)
5287b07f6d7cc3 Kees Cook      2020-10-02  100  			break;
0fa8e084648779 Kees Cook      2020-10-02  101  		copied += bytes;
5287b07f6d7cc3 Kees Cook      2020-10-02  102  	}
5287b07f6d7cc3 Kees Cook      2020-10-02  103  
0fa8e084648779 Kees Cook      2020-10-02  104  	if (whole_file) {
5287b07f6d7cc3 Kees Cook      2020-10-02  105  		if (pos != i_size) {
5287b07f6d7cc3 Kees Cook      2020-10-02  106  			ret = -EIO;
5287b07f6d7cc3 Kees Cook      2020-10-02  107  			goto out_free;
5287b07f6d7cc3 Kees Cook      2020-10-02  108  		}
5287b07f6d7cc3 Kees Cook      2020-10-02  109  
5287b07f6d7cc3 Kees Cook      2020-10-02  110  		ret = security_kernel_post_read_file(file, *buf, i_size, id);
0fa8e084648779 Kees Cook      2020-10-02  111  	}
5287b07f6d7cc3 Kees Cook      2020-10-02  112  
5287b07f6d7cc3 Kees Cook      2020-10-02  113  out_free:
5287b07f6d7cc3 Kees Cook      2020-10-02  114  	if (ret < 0) {
5287b07f6d7cc3 Kees Cook      2020-10-02  115  		if (allocated) {
5287b07f6d7cc3 Kees Cook      2020-10-02  116  			vfree(*buf);
5287b07f6d7cc3 Kees Cook      2020-10-02  117  			*buf = NULL;
5287b07f6d7cc3 Kees Cook      2020-10-02  118  		}
5287b07f6d7cc3 Kees Cook      2020-10-02  119  	}
5287b07f6d7cc3 Kees Cook      2020-10-02  120  
5287b07f6d7cc3 Kees Cook      2020-10-02  121  out:
5287b07f6d7cc3 Kees Cook      2020-10-02  122  	allow_write_access(file);
0fa8e084648779 Kees Cook      2020-10-02  123  	return ret == 0 ? copied : ret;
5287b07f6d7cc3 Kees Cook      2020-10-02  124  }
5287b07f6d7cc3 Kees Cook      2020-10-02  125  EXPORT_SYMBOL_GPL(kernel_read_file);
5287b07f6d7cc3 Kees Cook      2020-10-02  126  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2022-06-08  4:48 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-27  2:55 [PATCH v2 0/2] Allow to kexec with initramfs larger than 2G Pasha Tatashin
2022-05-27  2:55 ` Pasha Tatashin
2022-05-27  2:55 ` [PATCH v2 1/2] fs/kernel_read_file: Allow to read files up-to ssize_t Pasha Tatashin
2022-05-27  2:55   ` Pasha Tatashin
2022-06-06  2:45   ` Baoquan He
2022-06-06  2:45     ` Baoquan He
2022-06-07 15:52     ` Pasha Tatashin
2022-06-07 15:52       ` Pasha Tatashin
2022-06-08  0:57       ` Baoquan He
2022-06-08  0:57         ` Baoquan He
2022-06-08  0:58   ` Baoquan He
2022-06-08  0:58     ` Baoquan He
2022-05-27  2:55 ` [PATCH v2 2/2] kexec_file: Increase maximum file size to 4G Pasha Tatashin
2022-05-27  2:55   ` Pasha Tatashin
2022-06-06  2:56   ` Baoquan He
2022-06-06  2:56     ` Baoquan He
2022-06-07 16:02     ` Pasha Tatashin
2022-06-07 16:02       ` Pasha Tatashin
2022-06-08  0:28       ` Baoquan He
2022-06-08  0:28         ` Baoquan He
2022-05-28  3:06 [PATCH v2 1/2] fs/kernel_read_file: Allow to read files up-to ssize_t kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.