fd-util: introduce fd_verify_safe_flags
As per https://github.com/systemd/systemd/pull/31419#discussion_r1496921074
This commit is contained in:
@@ -913,6 +913,36 @@ int fd_is_opath(int fd) {
|
||||
return FLAGS_SET(r, O_PATH);
|
||||
}
|
||||
|
||||
int fd_verify_safe_flags(int fd) {
|
||||
int flags, unexpected_flags;
|
||||
|
||||
/* Check if an extrinsic fd is safe to work on (by a privileged service). This ensures that clients
|
||||
* can't trick a privileged service into giving access to a file the client doesn't already have
|
||||
* access to (especially via something like O_PATH).
|
||||
*
|
||||
* O_NOFOLLOW: For some reason the kernel will return this flag from fcntl; it doesn't go away
|
||||
* immediately after open(). It should have no effect whatsoever to an already-opened FD,
|
||||
* and since we refuse O_PATH it should be safe.
|
||||
*
|
||||
* RAW_O_LARGEFILE: glibc secretly sets this and neglects to hide it from us if we call fcntl.
|
||||
* See comment in missing_fcntl.h for more details about this.
|
||||
*/
|
||||
|
||||
assert(fd >= 0);
|
||||
|
||||
flags = fcntl(fd, F_GETFL);
|
||||
if (flags < 0)
|
||||
return -errno;
|
||||
|
||||
unexpected_flags = flags & ~(O_ACCMODE|O_NOFOLLOW|RAW_O_LARGEFILE);
|
||||
if (unexpected_flags != 0)
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EREMOTEIO),
|
||||
"Unexpected flags set for extrinsic fd: 0%o",
|
||||
(unsigned) unexpected_flags);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int read_nr_open(void) {
|
||||
_cleanup_free_ char *nr_open = NULL;
|
||||
int r;
|
||||
|
||||
Reference in New Issue
Block a user