License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2011-01-29 13:13:26 +00:00
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/namei.h>
|
|
|
|
#include <linux/exportfs.h>
|
2011-01-29 13:13:26 +00:00
|
|
|
#include <linux/fs_struct.h>
|
|
|
|
#include <linux/fsnotify.h>
|
2011-04-14 22:22:16 +00:00
|
|
|
#include <linux/personality.h>
|
2016-12-24 19:46:01 +00:00
|
|
|
#include <linux/uaccess.h>
|
2017-04-08 22:16:56 +00:00
|
|
|
#include <linux/compat.h>
|
2011-01-29 13:13:26 +00:00
|
|
|
#include "internal.h"
|
2011-11-25 05:50:41 +00:00
|
|
|
#include "mount.h"
|
2011-01-29 13:13:26 +00:00
|
|
|
|
2022-08-04 16:46:18 +00:00
|
|
|
static long do_sys_name_to_handle(const struct path *path,
|
2011-01-29 13:13:26 +00:00
|
|
|
struct file_handle __user *ufh,
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
void __user *mnt_id, bool unique_mntid,
|
|
|
|
int fh_flags)
|
2011-01-29 13:13:26 +00:00
|
|
|
{
|
|
|
|
long retval;
|
|
|
|
struct file_handle f_handle;
|
|
|
|
int handle_dwords, handle_bytes;
|
|
|
|
struct file_handle *handle = NULL;
|
|
|
|
|
|
|
|
/*
|
2023-05-02 12:48:16 +00:00
|
|
|
* We need to make sure whether the file system support decoding of
|
|
|
|
* the file handle if decodeable file handle was requested.
|
2011-01-29 13:13:26 +00:00
|
|
|
*/
|
2023-10-23 18:07:58 +00:00
|
|
|
if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
|
2011-01-29 13:13:26 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2024-10-11 09:00:22 +00:00
|
|
|
/*
|
|
|
|
* A request to encode a connectable handle for a disconnected dentry
|
|
|
|
* is unexpected since AT_EMPTY_PATH is not allowed.
|
|
|
|
*/
|
|
|
|
if (fh_flags & EXPORT_FH_CONNECTABLE &&
|
|
|
|
WARN_ON(path->dentry->d_flags & DCACHE_DISCONNECTED))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2011-01-29 13:13:26 +00:00
|
|
|
if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (f_handle.handle_bytes > MAX_HANDLE_SZ)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2024-03-26 01:34:01 +00:00
|
|
|
handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
|
2011-01-29 13:13:26 +00:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!handle)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-09-20 01:48:00 +00:00
|
|
|
/* convert handle size to multiple of sizeof(u32) */
|
2011-01-29 13:13:26 +00:00
|
|
|
handle_dwords = f_handle.handle_bytes >> 2;
|
|
|
|
|
2024-10-11 09:00:22 +00:00
|
|
|
/* Encode a possibly decodeable/connectable file handle */
|
2011-01-29 13:13:26 +00:00
|
|
|
retval = exportfs_encode_fh(path->dentry,
|
|
|
|
(struct fid *)handle->f_handle,
|
2023-05-02 12:48:16 +00:00
|
|
|
&handle_dwords, fh_flags);
|
2011-01-29 13:13:26 +00:00
|
|
|
handle->handle_type = retval;
|
|
|
|
/* convert handle size to bytes */
|
|
|
|
handle_bytes = handle_dwords * sizeof(u32);
|
|
|
|
handle->handle_bytes = handle_bytes;
|
|
|
|
if ((handle->handle_bytes > f_handle.handle_bytes) ||
|
2023-05-24 15:48:25 +00:00
|
|
|
(retval == FILEID_INVALID) || (retval < 0)) {
|
2011-01-29 13:13:26 +00:00
|
|
|
/* As per old exportfs_encode_fh documentation
|
|
|
|
* we could return ENOSPC to indicate overflow
|
|
|
|
* But file system returned 255 always. So handle
|
|
|
|
* both the values
|
|
|
|
*/
|
2023-05-24 15:48:25 +00:00
|
|
|
if (retval == FILEID_INVALID || retval == -ENOSPC)
|
|
|
|
retval = -EOVERFLOW;
|
2011-01-29 13:13:26 +00:00
|
|
|
/*
|
|
|
|
* set the handle size to zero so we copy only
|
|
|
|
* non variable part of the file_handle
|
|
|
|
*/
|
|
|
|
handle_bytes = 0;
|
2024-10-11 09:00:22 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* When asked to encode a connectable file handle, encode this
|
|
|
|
* property in the file handle itself, so that we later know
|
|
|
|
* how to decode it.
|
|
|
|
* For sanity, also encode in the file handle if the encoded
|
|
|
|
* object is a directory and verify this during decode, because
|
|
|
|
* decoding directory file handles is quite different than
|
|
|
|
* decoding connectable non-directory file handles.
|
|
|
|
*/
|
|
|
|
if (fh_flags & EXPORT_FH_CONNECTABLE) {
|
|
|
|
handle->handle_type |= FILEID_IS_CONNECTABLE;
|
|
|
|
if (d_is_dir(path->dentry))
|
|
|
|
fh_flags |= FILEID_IS_DIR;
|
|
|
|
}
|
2011-01-29 13:13:26 +00:00
|
|
|
retval = 0;
|
2024-10-11 09:00:22 +00:00
|
|
|
}
|
2011-01-29 13:13:26 +00:00
|
|
|
/* copy the mount id */
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
if (unique_mntid) {
|
|
|
|
if (put_user(real_mount(path->mnt)->mnt_id_unique,
|
|
|
|
(u64 __user *) mnt_id))
|
|
|
|
retval = -EFAULT;
|
|
|
|
} else {
|
|
|
|
if (put_user(real_mount(path->mnt)->mnt_id,
|
|
|
|
(int __user *) mnt_id))
|
|
|
|
retval = -EFAULT;
|
|
|
|
}
|
|
|
|
/* copy the handle */
|
|
|
|
if (retval != -EFAULT &&
|
|
|
|
copy_to_user(ufh, handle,
|
|
|
|
struct_size(handle, f_handle, handle_bytes)))
|
2011-01-29 13:13:26 +00:00
|
|
|
retval = -EFAULT;
|
|
|
|
kfree(handle);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sys_name_to_handle_at: convert name to handle
|
|
|
|
* @dfd: directory relative to which name is interpreted if not absolute
|
|
|
|
* @name: name that should be converted to handle.
|
|
|
|
* @handle: resulting file handle
|
|
|
|
* @mnt_id: mount id of the file system containing the file
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
* (u64 if AT_HANDLE_MNT_ID_UNIQUE, otherwise int)
|
2011-01-29 13:13:26 +00:00
|
|
|
* @flag: flag value to indicate whether to follow symlink or not
|
2023-05-02 12:48:16 +00:00
|
|
|
* and whether a decodable file handle is required.
|
2011-01-29 13:13:26 +00:00
|
|
|
*
|
|
|
|
* @handle->handle_size indicate the space available to store the
|
|
|
|
* variable part of the file handle in bytes. If there is not
|
|
|
|
* enough space, the field is updated to return the minimum
|
|
|
|
* value required.
|
|
|
|
*/
|
|
|
|
SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
struct file_handle __user *, handle, void __user *, mnt_id,
|
2011-01-29 13:13:26 +00:00
|
|
|
int, flag)
|
|
|
|
{
|
|
|
|
struct path path;
|
|
|
|
int lookup_flags;
|
2024-10-11 09:00:22 +00:00
|
|
|
int fh_flags = 0;
|
2011-01-29 13:13:26 +00:00
|
|
|
int err;
|
|
|
|
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID |
|
2024-10-11 09:00:22 +00:00
|
|
|
AT_HANDLE_MNT_ID_UNIQUE | AT_HANDLE_CONNECTABLE))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AT_HANDLE_FID means there is no intention to decode file handle
|
|
|
|
* AT_HANDLE_CONNECTABLE means there is an intention to decode a
|
|
|
|
* connected fd (with known path), so these flags are conflicting.
|
|
|
|
* AT_EMPTY_PATH could be used along with a dfd that refers to a
|
|
|
|
* disconnected non-directory, which cannot be used to encode a
|
|
|
|
* connectable file handle, because its parent is unknown.
|
|
|
|
*/
|
|
|
|
if (flag & AT_HANDLE_CONNECTABLE &&
|
|
|
|
flag & (AT_HANDLE_FID | AT_EMPTY_PATH))
|
2011-01-29 13:13:26 +00:00
|
|
|
return -EINVAL;
|
2024-10-11 09:00:22 +00:00
|
|
|
else if (flag & AT_HANDLE_FID)
|
|
|
|
fh_flags |= EXPORT_FH_FID;
|
|
|
|
else if (flag & AT_HANDLE_CONNECTABLE)
|
|
|
|
fh_flags |= EXPORT_FH_CONNECTABLE;
|
2011-01-29 13:13:26 +00:00
|
|
|
|
|
|
|
lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
|
|
|
|
if (flag & AT_EMPTY_PATH)
|
|
|
|
lookup_flags |= LOOKUP_EMPTY;
|
|
|
|
err = user_path_at(dfd, name, lookup_flags, &path);
|
|
|
|
if (!err) {
|
fhandle: expose u64 mount id to name_to_handle_at(2)
Now that we provide a unique 64-bit mount ID interface in statx(2), we
can now provide a race-free way for name_to_handle_at(2) to provide a
file handle and corresponding mount without needing to worry about
racing with /proc/mountinfo parsing or having to open a file just to do
statx(2).
While this is not necessary if you are using AT_EMPTY_PATH and don't
care about an extra statx(2) call, users that pass full paths into
name_to_handle_at(2) need to know which mount the file handle comes from
(to make sure they don't try to open_by_handle_at a file handle from a
different filesystem) and switching to AT_EMPTY_PATH would require
allocating a file for every name_to_handle_at(2) call, turning
err = name_to_handle_at(-EBADF, "/foo/bar/baz", &handle, &mntid,
AT_HANDLE_MNT_ID_UNIQUE);
into
int fd = openat(-EBADF, "/foo/bar/baz", O_PATH | O_CLOEXEC);
err1 = name_to_handle_at(fd, "", &handle, &unused_mntid, AT_EMPTY_PATH);
err2 = statx(fd, "", AT_EMPTY_PATH, STATX_MNT_ID_UNIQUE, &statxbuf);
mntid = statxbuf.stx_mnt_id;
close(fd);
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Link: https://lore.kernel.org/r/20240828-exportfs-u64-mount-id-v3-2-10c2c4c16708@cyphar.com
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Christian Brauner <brauner@kernel.org>
2024-08-28 10:19:43 +00:00
|
|
|
err = do_sys_name_to_handle(&path, handle, mnt_id,
|
|
|
|
flag & AT_HANDLE_MNT_ID_UNIQUE,
|
|
|
|
fh_flags);
|
2011-01-29 13:13:26 +00:00
|
|
|
path_put(&path);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
2011-01-29 13:13:26 +00:00
|
|
|
|
2024-05-24 10:19:39 +00:00
|
|
|
static int get_path_from_fd(int fd, struct path *root)
|
2011-01-29 13:13:26 +00:00
|
|
|
{
|
|
|
|
if (fd == AT_FDCWD) {
|
|
|
|
struct fs_struct *fs = current->fs;
|
|
|
|
spin_lock(&fs->lock);
|
2024-05-24 10:19:39 +00:00
|
|
|
*root = fs->pwd;
|
|
|
|
path_get(root);
|
2011-01-29 13:13:26 +00:00
|
|
|
spin_unlock(&fs->lock);
|
|
|
|
} else {
|
2024-07-20 00:17:58 +00:00
|
|
|
CLASS(fd, f)(fd);
|
|
|
|
if (fd_empty(f))
|
2024-05-24 10:19:39 +00:00
|
|
|
return -EBADF;
|
2024-05-31 18:12:01 +00:00
|
|
|
*root = fd_file(f)->f_path;
|
2024-05-24 10:19:39 +00:00
|
|
|
path_get(root);
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
2024-05-24 10:19:39 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
|
|
|
|
{
|
2024-05-24 10:19:39 +00:00
|
|
|
struct handle_to_path_ctx *ctx = context;
|
|
|
|
struct user_namespace *user_ns = current_user_ns();
|
|
|
|
struct dentry *d, *root = ctx->root.dentry;
|
|
|
|
struct mnt_idmap *idmap = mnt_idmap(ctx->root.mnt);
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* Old permission model with global CAP_DAC_READ_SEARCH. */
|
|
|
|
if (!ctx->flags)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's racy as we're not taking rename_lock but we're able to ignore
|
|
|
|
* permissions and we just need an approximation whether we were able
|
|
|
|
* to follow a path to the file.
|
|
|
|
*
|
|
|
|
* It's also potentially expensive on some filesystems especially if
|
|
|
|
* there is a deep path.
|
|
|
|
*/
|
|
|
|
d = dget(dentry);
|
|
|
|
while (d != root && !IS_ROOT(d)) {
|
|
|
|
struct dentry *parent = dget_parent(d);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We know that we have the ability to override DAC permissions
|
|
|
|
* as we've verified this earlier via CAP_DAC_READ_SEARCH. But
|
|
|
|
* we also need to make sure that there aren't any unmapped
|
|
|
|
* inodes in the path that would prevent us from reaching the
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
if (!privileged_wrt_inode_uidgid(user_ns, idmap,
|
|
|
|
d_inode(parent))) {
|
|
|
|
dput(d);
|
|
|
|
dput(parent);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
dput(d);
|
|
|
|
d = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ctx->flags & HANDLE_CHECK_SUBTREE) || d == root)
|
|
|
|
retval = 1;
|
2024-10-11 09:00:23 +00:00
|
|
|
/*
|
|
|
|
* exportfs_decode_fh_raw() does not call acceptable() callback with
|
|
|
|
* a disconnected directory dentry, so we should have reached either
|
|
|
|
* mount fd directory or sb root.
|
|
|
|
*/
|
|
|
|
if (ctx->fh_flags & EXPORT_FH_DIR_ONLY)
|
|
|
|
WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
|
2024-05-24 10:19:39 +00:00
|
|
|
dput(d);
|
|
|
|
return retval;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 10:19:39 +00:00
|
|
|
static int do_handle_to_path(struct file_handle *handle, struct path *path,
|
|
|
|
struct handle_to_path_ctx *ctx)
|
2011-01-29 13:13:26 +00:00
|
|
|
{
|
|
|
|
int handle_dwords;
|
2024-05-24 10:19:39 +00:00
|
|
|
struct vfsmount *mnt = ctx->root.mnt;
|
2024-11-29 13:38:01 +00:00
|
|
|
struct dentry *dentry;
|
2011-01-29 13:13:26 +00:00
|
|
|
|
|
|
|
/* change the handle size to multiple of sizeof(u32) */
|
|
|
|
handle_dwords = handle->handle_bytes >> 2;
|
2024-11-29 13:38:01 +00:00
|
|
|
dentry = exportfs_decode_fh_raw(mnt, (struct fid *)handle->f_handle,
|
|
|
|
handle_dwords, handle->handle_type,
|
|
|
|
ctx->fh_flags, vfs_dentry_acceptable,
|
|
|
|
ctx);
|
|
|
|
if (IS_ERR_OR_NULL(dentry)) {
|
|
|
|
if (dentry == ERR_PTR(-ENOMEM))
|
2024-05-24 10:19:39 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
return -ESTALE;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
2024-11-29 13:38:01 +00:00
|
|
|
path->dentry = dentry;
|
2024-05-24 10:19:39 +00:00
|
|
|
path->mnt = mntget(mnt);
|
2011-01-29 13:13:26 +00:00
|
|
|
return 0;
|
2024-05-24 10:19:39 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 13:38:04 +00:00
|
|
|
static inline int may_decode_fh(struct handle_to_path_ctx *ctx,
|
|
|
|
unsigned int o_flags)
|
2024-05-24 10:19:39 +00:00
|
|
|
{
|
|
|
|
struct path *root = &ctx->root;
|
|
|
|
|
2024-11-29 13:38:03 +00:00
|
|
|
if (capable(CAP_DAC_READ_SEARCH))
|
2024-11-29 13:38:04 +00:00
|
|
|
return 0;
|
2024-11-29 13:38:03 +00:00
|
|
|
|
2024-05-24 10:19:39 +00:00
|
|
|
/*
|
2024-11-29 13:38:03 +00:00
|
|
|
* Allow relaxed permissions of file handles if the caller has
|
|
|
|
* the ability to mount the filesystem or create a bind-mount of
|
|
|
|
* the provided @mountdirfd.
|
|
|
|
*
|
|
|
|
* In both cases the caller may be able to get an unobstructed
|
|
|
|
* way to the encoded file handle. If the caller is only able to
|
|
|
|
* create a bind-mount we need to verify that there are no
|
|
|
|
* locked mounts on top of it that could prevent us from getting
|
|
|
|
* to the encoded file.
|
|
|
|
*
|
|
|
|
* In principle, locked mounts can prevent the caller from
|
|
|
|
* mounting the filesystem but that only applies to procfs and
|
|
|
|
* sysfs neither of which support decoding file handles.
|
|
|
|
*
|
|
|
|
* Restrict to O_DIRECTORY to provide a deterministic API that
|
|
|
|
* avoids a confusing api in the face of disconnected non-dir
|
|
|
|
* dentries.
|
2024-05-24 10:19:39 +00:00
|
|
|
*
|
|
|
|
* There's only one dentry for each directory inode (VFS rule)...
|
|
|
|
*/
|
|
|
|
if (!(o_flags & O_DIRECTORY))
|
2024-11-29 13:38:04 +00:00
|
|
|
return -EPERM;
|
2024-05-24 10:19:39 +00:00
|
|
|
|
|
|
|
if (ns_capable(root->mnt->mnt_sb->s_user_ns, CAP_SYS_ADMIN))
|
|
|
|
ctx->flags = HANDLE_CHECK_PERMS;
|
|
|
|
else if (is_mounted(root->mnt) &&
|
|
|
|
ns_capable(real_mount(root->mnt)->mnt_ns->user_ns,
|
|
|
|
CAP_SYS_ADMIN) &&
|
|
|
|
!has_locked_children(real_mount(root->mnt), root->dentry))
|
|
|
|
ctx->flags = HANDLE_CHECK_PERMS | HANDLE_CHECK_SUBTREE;
|
|
|
|
else
|
2024-11-29 13:38:04 +00:00
|
|
|
return -EPERM;
|
2024-05-24 10:19:39 +00:00
|
|
|
|
|
|
|
/* Are we able to override DAC permissions? */
|
|
|
|
if (!ns_capable(current_user_ns(), CAP_DAC_READ_SEARCH))
|
2024-11-29 13:38:04 +00:00
|
|
|
return -EPERM;
|
2024-05-24 10:19:39 +00:00
|
|
|
|
|
|
|
ctx->fh_flags = EXPORT_FH_DIR_ONLY;
|
2024-11-29 13:38:04 +00:00
|
|
|
return 0;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
|
2024-05-24 10:19:39 +00:00
|
|
|
struct path *path, unsigned int o_flags)
|
2011-01-29 13:13:26 +00:00
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
struct file_handle f_handle;
|
|
|
|
struct file_handle *handle = NULL;
|
2024-05-24 10:19:39 +00:00
|
|
|
struct handle_to_path_ctx ctx = {};
|
2024-11-29 13:38:04 +00:00
|
|
|
const struct export_operations *eops;
|
2011-01-29 13:13:26 +00:00
|
|
|
|
2024-05-24 10:19:39 +00:00
|
|
|
retval = get_path_from_fd(mountdirfd, &ctx.root);
|
|
|
|
if (retval)
|
2011-01-29 13:13:26 +00:00
|
|
|
goto out_err;
|
2024-05-24 10:19:39 +00:00
|
|
|
|
2024-11-29 13:38:04 +00:00
|
|
|
eops = ctx.root.mnt->mnt_sb->s_export_op;
|
|
|
|
if (eops && eops->permission)
|
|
|
|
retval = eops->permission(&ctx, o_flags);
|
|
|
|
else
|
|
|
|
retval = may_decode_fh(&ctx, o_flags);
|
|
|
|
if (retval)
|
2024-05-24 10:19:39 +00:00
|
|
|
goto out_path;
|
|
|
|
|
2011-01-29 13:13:26 +00:00
|
|
|
if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
|
|
|
|
retval = -EFAULT;
|
2024-05-24 10:19:39 +00:00
|
|
|
goto out_path;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
|
|
|
|
(f_handle.handle_bytes == 0)) {
|
|
|
|
retval = -EINVAL;
|
2024-05-24 10:19:39 +00:00
|
|
|
goto out_path;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
2024-10-11 09:00:21 +00:00
|
|
|
if (f_handle.handle_type < 0 ||
|
|
|
|
FILEID_USER_FLAGS(f_handle.handle_type) & ~FILEID_VALID_USER_FLAGS) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto out_path;
|
|
|
|
}
|
2024-10-11 09:00:23 +00:00
|
|
|
|
2024-03-26 01:34:01 +00:00
|
|
|
handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
|
2011-01-29 13:13:26 +00:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!handle) {
|
|
|
|
retval = -ENOMEM;
|
2024-05-24 10:19:39 +00:00
|
|
|
goto out_path;
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
/* copy the full handle */
|
2015-01-28 20:30:43 +00:00
|
|
|
*handle = f_handle;
|
|
|
|
if (copy_from_user(&handle->f_handle,
|
|
|
|
&ufh->f_handle,
|
2011-01-29 13:13:26 +00:00
|
|
|
f_handle.handle_bytes)) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto out_handle;
|
|
|
|
}
|
|
|
|
|
2024-10-11 09:00:23 +00:00
|
|
|
/*
|
|
|
|
* If handle was encoded with AT_HANDLE_CONNECTABLE, verify that we
|
|
|
|
* are decoding an fd with connected path, which is accessible from
|
|
|
|
* the mount fd path.
|
|
|
|
*/
|
|
|
|
if (f_handle.handle_type & FILEID_IS_CONNECTABLE) {
|
|
|
|
ctx.fh_flags |= EXPORT_FH_CONNECTABLE;
|
|
|
|
ctx.flags |= HANDLE_CHECK_SUBTREE;
|
|
|
|
}
|
|
|
|
if (f_handle.handle_type & FILEID_IS_DIR)
|
|
|
|
ctx.fh_flags |= EXPORT_FH_DIR_ONLY;
|
2024-10-11 09:00:21 +00:00
|
|
|
/* Filesystem code should not be exposed to user flags */
|
|
|
|
handle->handle_type &= ~FILEID_USER_FLAGS_MASK;
|
2024-05-24 10:19:39 +00:00
|
|
|
retval = do_handle_to_path(handle, path, &ctx);
|
2011-01-29 13:13:26 +00:00
|
|
|
|
|
|
|
out_handle:
|
|
|
|
kfree(handle);
|
2024-05-24 10:19:39 +00:00
|
|
|
out_path:
|
|
|
|
path_put(&ctx.root);
|
2011-01-29 13:13:26 +00:00
|
|
|
out_err:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2017-10-14 17:18:33 +00:00
|
|
|
static long do_handle_open(int mountdirfd, struct file_handle __user *ufh,
|
|
|
|
int open_flag)
|
2011-01-29 13:13:26 +00:00
|
|
|
{
|
|
|
|
long retval = 0;
|
2024-11-29 13:38:01 +00:00
|
|
|
struct path path __free(path_put) = {};
|
2011-01-29 13:13:26 +00:00
|
|
|
struct file *file;
|
2024-11-29 13:38:02 +00:00
|
|
|
const struct export_operations *eops;
|
2011-01-29 13:13:26 +00:00
|
|
|
|
2024-05-24 10:19:39 +00:00
|
|
|
retval = handle_to_path(mountdirfd, ufh, &path, open_flag);
|
2011-01-29 13:13:26 +00:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
2024-11-29 13:38:01 +00:00
|
|
|
CLASS(get_unused_fd, fd)(O_CLOEXEC);
|
|
|
|
if (fd < 0)
|
2011-01-29 13:13:26 +00:00
|
|
|
return fd;
|
2024-11-29 13:38:01 +00:00
|
|
|
|
2024-11-29 13:38:02 +00:00
|
|
|
eops = path.mnt->mnt_sb->s_export_op;
|
|
|
|
if (eops->open)
|
|
|
|
file = eops->open(&path, open_flag);
|
|
|
|
else
|
|
|
|
file = file_open_root(&path, "", open_flag, 0);
|
2024-11-29 13:38:01 +00:00
|
|
|
if (IS_ERR(file))
|
|
|
|
return PTR_ERR(file);
|
|
|
|
|
|
|
|
fd_install(fd, file);
|
|
|
|
return take_fd(fd);
|
2011-01-29 13:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sys_open_by_handle_at: Open the file handle
|
|
|
|
* @mountdirfd: directory file descriptor
|
|
|
|
* @handle: file handle to be opened
|
2019-08-07 23:22:34 +00:00
|
|
|
* @flags: open flags.
|
2011-01-29 13:13:26 +00:00
|
|
|
*
|
|
|
|
* @mountdirfd indicate the directory file descriptor
|
|
|
|
* of the mount point. file handle is decoded relative
|
|
|
|
* to the vfsmount pointed by the @mountdirfd. @flags
|
|
|
|
* value is same as the open(2) flags.
|
|
|
|
*/
|
|
|
|
SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
|
|
|
|
struct file_handle __user *, handle,
|
|
|
|
int, flags)
|
|
|
|
{
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (force_o_largefile())
|
|
|
|
flags |= O_LARGEFILE;
|
|
|
|
|
|
|
|
ret = do_handle_open(mountdirfd, handle, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-04-08 22:16:56 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
/*
|
|
|
|
* Exactly like fs/open.c:sys_open_by_handle_at(), except that it
|
|
|
|
* doesn't set the O_LARGEFILE flag.
|
|
|
|
*/
|
|
|
|
COMPAT_SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
|
|
|
|
struct file_handle __user *, handle, int, flags)
|
|
|
|
{
|
|
|
|
return do_handle_open(mountdirfd, handle, flags);
|
|
|
|
}
|
|
|
|
#endif
|