From 732215a83f83eae3bdb56516fd617a258c813e44 Mon Sep 17 00:00:00 2001 From: Christian Helmuth Date: Fri, 4 Oct 2019 15:48:30 +0200 Subject: [PATCH] libc: limit fcntl(F_SETFL) to file-status flags This fixes unintended (and unpermitted) changes of O_ACCMODE bits. --- repos/libports/src/lib/libc/vfs_plugin.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/repos/libports/src/lib/libc/vfs_plugin.cc b/repos/libports/src/lib/libc/vfs_plugin.cc index a3b89254ad..00a9fb75b9 100644 --- a/repos/libports/src/lib/libc/vfs_plugin.cc +++ b/repos/libports/src/lib/libc/vfs_plugin.cc @@ -1108,7 +1108,11 @@ int Libc::Vfs_plugin::fcntl(File_descriptor *fd, int cmd, long arg) case F_SETFD: fd->cloexec = arg == FD_CLOEXEC; return 0; case F_GETFL: return fd->flags; - case F_SETFL: fd->flags = arg; return 0; + case F_SETFL: { + /* only the specified flags may be changed */ + long const mask = (O_NONBLOCK | O_APPEND | O_ASYNC | O_FSYNC); + fd->flags = (fd->flags & ~mask) | (arg & mask); + } return 0; default: break;