file: Introduce iterate_fd_locked
Subsystems (3)
RFC 1/3 file: Introduce iterate_fd_locked
Callers holding the files->file_lock lock can call iterate_fd_locked instead of iterate_fd
--- fs/file.c | 21 +++++++++++++++------ include/linux/fdtable.h | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-)
fs/file.c +15 -6
@@ -1295,15 +1295,12 @@ int f_dupfd(unsigned int from, struct file *file, unsigned flags)return err; } -int iterate_fd (struct files_struct *files,unsigned n, -int (*f)(const void *,struct file *,unsigned ), -const void *p) +int iterate_fd_locked (struct files_struct *files,unsigned n, +int (*f)(const void *,struct file *,unsigned ), +const void *p) {struct fdtable *fdt;int res =0 ; -if (!files) -return 0 ; -spin_lock (&files->file_lock);for (fdt =files_fdtable (files); n < fdt->max_fds; n++) {struct file *file; file =rcu_dereference_check_fdtable (files, fdt->fd[n]);
@@ -1313,6 +1310,18 @@ int iterate_fd(struct files_struct *files, unsigned n,if (res)break ; } +return res; +} + +int iterate_fd (struct files_struct *files,unsigned n, +int (*f)(const void *,struct file *,unsigned ), +const void *p) +{ +int res =0 ; +if (!files) +return 0 ; +spin_lock (&files->file_lock); + res =iterate_fd_locked (files, n, f, p);spin_unlock (&files->file_lock);return res; }
include/linux/fdtable.h +3 -0
@@ -122,6 +122,9 @@ void do_close_on_exec(struct files_struct *);int iterate_fd (struct files_struct *,unsigned ,int (*)(const void *,struct file *,unsigned ),const void *); +int iterate_fd_locked (struct files_struct *,unsigned , +int (*)(const void *,struct file *,unsigned ), +const void *);extern int close_fd (unsigned int fd);extern int __close_range (unsigned int fd,unsigned int max_fd,unsigned int flags);
Signature · 1 line
2.34.1
RFC 2/3 file: allow callers to free the old file descriptor after dup2
Allow callers of do_dup2 to free the old file descriptor in case they need to make additional operations on it.
--- fs/file.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
fs/file.c +12 -4
@@ -1086,7 +1086,7 @@ bool get_close_on_exec(unsigned int fd) }static int do_dup2 (struct files_struct *files, -struct file *file,unsigned fd,unsigned flags) +struct file *file,unsigned fd,struct file **fdfile,unsigned flags) __releases(&files->file_lock) {struct file *tofree;
@@ -1120,7 +1120,7 @@ __releases(&files->file_lock)spin_unlock (&files->file_lock);if (tofree) -filp_close (tofree, files); + *fdfile = tofree;return fd;
@@ -1132,6 +1132,7 @@ __releases(&files->file_lock)int replace_fd (unsigned fd,struct file *file,unsigned flags) {int err; +struct file *fdfile =NULL ;struct files_struct *files = current->files;if (!file)
@@ -1144,7 +1145,10 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags) err =expand_files (files, fd);if (unlikely (err <0 )) goto out_unlock; -return do_dup2 (files, file, fd, flags); + err =do_dup2 (files, file, fd, &fdfile, flags); +if (fdfile) +filp_close (fdfile, files); +return err; out_unlock:spin_unlock (&files->file_lock);
@@ -1216,6 +1220,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) {int err = -EBADF ;struct file *file; +struct file *fdfile =NULL ;struct files_struct *files = current->files;if ((flags & ~O_CLOEXEC ) !=0 )
@@ -1237,7 +1242,10 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags) goto Ebadf; goto out_unlock; } -return do_dup2 (files, file, newfd, flags); + err =do_dup2 (files, file, newfd, &fdfile, flags); +if (fdfile) +filp_close (fdfile, files); +return err; Ebadf: err = -EBADF ;
Signature · 1 line
2.34.1
RE: [RFC 2/3] file: allow callers to free the old file descriptor after dup2
From: aloktiagi
Quoted 4 lines
Sent: 02 March 2023 18:22
Allow callers of
do_dup2to free the old file descriptor in case they need to make additional operations on it.
That doesn't read right at all.
Whether or not this is a good idea (or can be done differently) the interface is horrid.
fs/file.c __releases(&files->file_lock) · line 1123
Quoted 3 lines
fs/file.c +1
-1if (tofree) -filp_close (tofree, files); + *fdfile = tofree;
Why not:
if (fdfile) [
*fdfile = tofree;
} else {
if (tofree)
filp_close(tofree, files);
}Then existing code just passes NULL and the caller can't 'forget' to intitalise fdfile.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Re: [RFC 2/3] file: allow callers to free the old file descriptor after dup2
Quoted 24 lines · David Laight
From: aloktiagi
Quoted 4 lines
Sent: 02 March 2023 18:22
Allow callers of
do_dup2to free the old file descriptor in case they need to make additional operations on it.That doesn't read right at all.
Whether or not this is a good idea (or can be done differently) the interface is horrid.
fs/file.c __releases(&files->file_lock) · line 1123
Quoted 3 lines
fs/file.c +1
-1if (tofree) -filp_close (tofree, files); + *fdfile = tofree;Why not:
if (fdfile) [ *fdfile = tofree; } else { if (tofree) filp_close(tofree, files); }
Quoted 9 lines
Then existing code just passes NULL and the caller can't 'forget' to intitalise fdfile.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
thank you for the review, I'll fix it in v2
RFC 3/3 file, epoll: Implement do_replace() and eventpoll_replace()
In reply to aloktiagiIntroduce a mechanism to replace a file linked in the epoll interface or a file that has been dup'ed by a file received via the replace_fd() interface.
eventpoll_replace() is called from do_replace() and finds all instances of the file to be replaced and replaces them with the new file.
do_replace() also replaces the file in the file descriptor table for all fd numbers referencing it with the new file.
We have a use case where multiple IPv6 only network namespaces can use a single IPv4 network namespace for IPv4 only egress connectivity by switching their sockets from IPv6 to IPv4 network namespace. This allows for migration of systems to IPv6 only while keeping their connectivity to IPv4 only destinations intact.
Today, we achieve this by setting up seccomp filter to intercept network system calls like connect() from a container in a container manager which runs in an IPv4 only network namespace. The container manager creates a new IPv4 connection and injects the new file descriptor through SECCOMP_NOTIFY_IOCTL_ADDFD replacing the original file descriptor from the connect() call. This does not work for cases where the original file descriptor is handed off to a system like epoll before the connect() call. After a new file descriptor is injected the original file descriptor being referenced by the epoll fd is not longer valid leading to failures. As a workaround the container manager when intercepting connect() loops through all open socket file descriptors to check if they are referencing the socket attempting the connect() and replace the reference with the to be injected file descriptor. This workaround is cumbersome and makes the solution prone to similar yet to be discovered issues.
The above change will enable us remove the workaround in the container manager and let the kernel handle the replacement correctly.
--- fs/eventpoll.c | 38 ++++++++ fs/file.c | 54 +++++++++++ include/linux/eventpoll.h | 18 ++++ include/linux/file.h | 1 + tools/testing/selftests/seccomp/seccomp_bpf.c | 97 +++++++++++++++++++ 5 files changed, 208 insertions(+)
fs/eventpoll.c +38 -0
@@ -935,6 +935,44 @@ void eventpoll_release_file(struct file *file)mutex_unlock (&epmutex); } +static int ep_insert (struct eventpoll *ep,const struct epoll_event *event, +struct file *tfile,int fd,int full_check); + +/* +* This is called from eventpoll_replace() to replace a linked file in the epoll +* interface with a new file received from another process. This is useful in +* cases where a process is trying to install a new file for an existing one +* that is linked in the epoll interface +*/ +void eventpoll_replace_file (struct file *toreplace,struct file *file) +{ +int fd; +struct eventpoll *ep; +struct epitem *epi; +struct hlist_node *next; +struct epoll_event event; + +if (!file_can_poll (file)) +return ; + +mutex_lock (&epmutex); +if (unlikely (!toreplace->f_ep)) { +mutex_unlock (&epmutex); +return ; + } + +hlist_for_each_entry_safe (epi, next, toreplace->f_ep, fllink) { + ep = epi->ep; +mutex_lock (&ep->mtx); + fd = epi->ffd.fd; + event = epi->event; +ep_remove (ep, epi); +ep_insert (ep, &event, file, fd,1 ); +mutex_unlock (&ep->mtx); + } +mutex_unlock (&epmutex); +} +static int ep_alloc (struct eventpoll **pep) {int error;
fs/file.c +54 -0
78 lines not on this page · raw message
include/linux/eventpoll.h +18 -0
@@ -25,6 +25,8 @@ struct file *get_epoll_tfile_raw_ptr(struct file *file, int tfd, unsigned long t/* Used to release the epoll bits inside the "struct file" */ void eventpoll_release_file (struct file *file); +void eventpoll_replace_file (struct file *toreplace,struct file *file); +/* * This is called from inside fs/file_table.c:__fput() to unlink files * from the eventpoll interface. We need to have this facility to cleanup
@@ -53,6 +55,22 @@ static inline void eventpoll_release(struct file *file)eventpoll_release_file (file); } + +/* +* This is called from fs/file.c:do_replace() to replace a linked file in the +* epoll interface with a new file received from another process. This is useful +* in cases where a process is trying to install a new file for an existing one +* that is linked in the epoll interface +*/ +static inline void eventpoll_replace (struct file *toreplace,struct file *file) +{ +/* +* toreplace is the file being replaced. Install the new file for the +* existing one that is linked in the epoll interface +*/ +eventpoll_replace_file (toreplace, file); +} +int do_epoll_ctl (int epfd,int op,int fd,struct epoll_event *epds,bool nonblock);
include/linux/file.h +1 -0
@@ -36,6 +36,7 @@ struct fd {struct file *file;unsigned int flags; }; +#define FDPUT_FPUT 1#define FDPUT_POS_UNLOCK 2
tools/testing/selftests/seccomp/seccomp_bpf.c +97 -0
109 lines not on this page · raw message
Signature · 1 line
2.34.1
kernel test robot
build botRe: [RFC 3/3] file, epoll: Implement do_replace() and eventpoll_replace()
[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…
Hi aloktiagi,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kees/for-next/seccomp]
[also build test ERROR on kees/for-next/pstore kees/for-next/kspp linus/master v6.2 next-20230302] [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#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230303-022310 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/seccomp patch link: https://lore.kernel.org/r/20230302182207.456311-3-aloktiagi%40gmail.com patch subject: [RFC 3/3] file, epoll: Implement do_replace() and eventpoll_replace() config: i386-tinyconfig (https://download.01.org/0day-ci/archive/20230303/[email protected]/config) compiler: gcc-11 (Debian 11.3.0-8) 11.3.0
reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/047f007e8195cfe7ed4191b97cf8d2c4ef309f08
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230303-022310 git checkout 047f007e8195cfe7ed4191b97cf8d2c4ef309f08
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
fs/file.c: In function 'do_replace':
>> fs/file.c:1172:31: error: 'struct file' has no member named 'f_ep'; did you mean 'f_op'?
1172 | if (fdfile && fdfile->f_ep) {
| ^~~~
| f_op
>> fs/file.c:1173:17: error: implicit declaration of function 'eventpoll_replace'; did you mean 'eventpoll_release'? [-Werror=implicit-function-declaration]
1173 | eventpoll_replace(fdfile, file);
| ^~~~~~~~~~~~~~~~~
| eventpoll_releasecc1: some warnings being treated as errors
vim +1172 fs/file.c
1155
1156 static void do_replace(struct files_struct *files,
1157 struct file *file, unsigned fd, struct file *fdfile)
1158 {
1159 unsigned n = 0;
1160 struct replace_filefd ffd = {
1161 .files = files,
1162 .fd = fd,
1163 .fdfile = fdfile,
1164 .file = file
1165 };
1166
1167 /*
1168 * Check if the file referenced by the fd number is linked to the epoll
1169 * interface. If yes, replace the reference with the received file in
1170 * the epoll interface.
1171 */
> 1172 if (fdfile && fdfile->f_ep) {
> 1173 eventpoll_replace(fdfile, file); 1174 }
1175 /*
1176 * Install the received file in the file descriptor table for all fd
1177 * numbers referencing the same file as the one we are trying to
1178 * replace. Do not install it for the fd number received since that is
1179 * handled in do_dup2()
1180 */
1181 iterate_fd_locked(files, n, do_replace_fd_array, &ffd);
1182 }
1183
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
kernel test robot
build botRe: [RFC 3/3] file, epoll: Implement do_replace() and eventpoll_replace()
In reply to aloktiagi[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…
Hi aloktiagi,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on kees/for-next/seccomp]
[also build test ERROR on kees/for-next/pstore kees/for-next/kspp linus/master v6.2 next-20230302] [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#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230303-022310 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/seccomp patch link: https://lore.kernel.org/r/20230302182207.456311-3-aloktiagi%40gmail.com patch subject: [RFC 3/3] file, epoll: Implement do_replace() and eventpoll_replace() config: arm-randconfig-r014-20230303 (https://download.01.org/0day-ci/archive/20230303/[email protected]/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 67409911353323ca5edf2049ef0df54132fa1ca7)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install arm cross compiling tool for clang build
# apt-get install binutils-arm-linux-gnueabi
# https://github.com/intel-lab-lkp/linux/commit/047f007e8195cfe7ed4191b97cf8d2c4ef309f08
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review aloktiagi/file-allow-callers-to-free-the-old-file-descriptor-after-dup2/20230303-022310 git checkout 047f007e8195cfe7ed4191b97cf8d2c4ef309f08
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash
If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> fs/file.c:1172:24: error: no member named 'f_ep' in 'struct file'; did you mean 'f_op'?
if (fdfile && fdfile->f_ep) {
^~~~
f_op
include/linux/fs.h:948:32: note: 'f_op' declared here
const struct file_operations *f_op;
^
>> fs/file.c:1173:3: error: call to undeclared function 'eventpoll_replace'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
eventpoll_replace(fdfile, file);
^
fs/file.c:1173:3: note: did you mean 'eventpoll_release'?
include/linux/eventpoll.h:85:20: note: 'eventpoll_release' declared here
static inline void eventpoll_release(struct file *file) {}
^2 errors generated.
vim +1172 fs/file.c
1155
1156 static void do_replace(struct files_struct *files,
1157 struct file *file, unsigned fd, struct file *fdfile)
1158 {
1159 unsigned n = 0;
1160 struct replace_filefd ffd = {
1161 .files = files,
1162 .fd = fd,
1163 .fdfile = fdfile,
1164 .file = file
1165 };
1166
1167 /*
1168 * Check if the file referenced by the fd number is linked to the epoll
1169 * interface. If yes, replace the reference with the received file in
1170 * the epoll interface.
1171 */
> 1172 if (fdfile && fdfile->f_ep) {
> 1173 eventpoll_replace(fdfile, file); 1174 }
1175 /*
1176 * Install the received file in the file descriptor table for all fd
1177 * numbers referencing the same file as the one we are trying to
1178 * replace. Do not install it for the fd number received since that is
1179 * handled in do_dup2()
1180 */
1181 iterate_fd_locked(files, n, do_replace_fd_array, &ffd);
1182 }
1183
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests