Skip to content

Commit 1738171

Browse files
committed
fs: Expand __receive_fd() to accept existing fd
Expand __receive_fd() with support for replace_fd() for the coming seccomp "addfd" ioctl(). Add new wrapper receive_fd_replace() for the new behavior and update existing wrappers to retain old behavior. Thanks to Colin Ian King <[email protected]> for pointing out an uninitialized variable exposure in an earlier version of this patch. Cc: Alexander Viro <[email protected]> Cc: Dmitry Kadashev <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: [email protected] Reviewed-by: Sargun Dhillon <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: Kees Cook <[email protected]>
1 parent 910d2f1 commit 1738171

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

fs/file.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
939939
/**
940940
* __receive_fd() - Install received file into file descriptor table
941941
*
942+
* @fd: fd to install into (if negative, a new fd will be allocated)
942943
* @file: struct file that was received from another process
943944
* @ufd: __user pointer to write new fd number to
944945
* @o_flags: the O_* flags to apply to the new fd entry
@@ -952,7 +953,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
952953
*
953954
* Returns newly install fd or -ve on error.
954955
*/
955-
int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
956+
int __receive_fd(int fd, struct file *file, int __user *ufd, unsigned int o_flags)
956957
{
957958
int new_fd;
958959
int error;
@@ -961,21 +962,33 @@ int __receive_fd(struct file *file, int __user *ufd, unsigned int o_flags)
961962
if (error)
962963
return error;
963964

964-
new_fd = get_unused_fd_flags(o_flags);
965-
if (new_fd < 0)
966-
return new_fd;
965+
if (fd < 0) {
966+
new_fd = get_unused_fd_flags(o_flags);
967+
if (new_fd < 0)
968+
return new_fd;
969+
} else {
970+
new_fd = fd;
971+
}
967972

968973
if (ufd) {
969974
error = put_user(new_fd, ufd);
970975
if (error) {
971-
put_unused_fd(new_fd);
976+
if (fd < 0)
977+
put_unused_fd(new_fd);
972978
return error;
973979
}
974980
}
975981

982+
if (fd < 0) {
983+
fd_install(new_fd, get_file(file));
984+
} else {
985+
error = replace_fd(new_fd, file, o_flags);
986+
if (error)
987+
return error;
988+
}
989+
976990
/* Bump the sock usage counts, if any. */
977991
__receive_sock(file);
978-
fd_install(new_fd, get_file(file));
979992
return new_fd;
980993
}
981994

include/linux/file.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,22 @@ extern void put_unused_fd(unsigned int fd);
9292

9393
extern void fd_install(unsigned int fd, struct file *file);
9494

95-
extern int __receive_fd(struct file *file, int __user *ufd,
95+
extern int __receive_fd(int fd, struct file *file, int __user *ufd,
9696
unsigned int o_flags);
9797
static inline int receive_fd_user(struct file *file, int __user *ufd,
9898
unsigned int o_flags)
9999
{
100100
if (ufd == NULL)
101101
return -EFAULT;
102-
return __receive_fd(file, ufd, o_flags);
102+
return __receive_fd(-1, file, ufd, o_flags);
103103
}
104104
static inline int receive_fd(struct file *file, unsigned int o_flags)
105105
{
106-
return __receive_fd(file, NULL, o_flags);
106+
return __receive_fd(-1, file, NULL, o_flags);
107+
}
108+
static inline int receive_fd_replace(int fd, struct file *file, unsigned int o_flags)
109+
{
110+
return __receive_fd(fd, file, NULL, o_flags);
107111
}
108112

109113
extern void flush_delayed_fput(void);

0 commit comments

Comments
 (0)