summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFabricio Voznika <fvoznika@google.com>2018-06-11 13:34:27 -0700
committerShentubot <shentubot@google.com>2018-06-11 13:35:21 -0700
commit7260363751915d21538c13b08b5bb6a48d0f4f8e (patch)
treed59fa807ec58bcda49eaf5153723421f95af9bde
parent032b0398a5a664c345c4868d5527846a1b6848db (diff)
Add O_TRUNC handling in openat
PiperOrigin-RevId: 200103677 Change-Id: I3efb565c30c64d35f8fd7b5c05ed78dcc2990c51
-rw-r--r--pkg/sentry/syscalls/linux/sys_file.go34
1 files changed, 19 insertions, 15 deletions
diff --git a/pkg/sentry/syscalls/linux/sys_file.go b/pkg/sentry/syscalls/linux/sys_file.go
index a2db9d4c9..e2980842f 100644
--- a/pkg/sentry/syscalls/linux/sys_file.go
+++ b/pkg/sentry/syscalls/linux/sys_file.go
@@ -147,21 +147,25 @@ func openAt(t *kernel.Task, dirFD kdefs.FD, addr usermem.Addr, flags uint) (fd u
}
fileFlags := linuxToFlags(flags)
- isDir := fs.IsDir(d.Inode.StableAttr)
-
- // If O_DIRECTORY is set, but the file is not a directory, then fail.
- if fileFlags.Directory && !isDir {
- return syserror.ENOTDIR
- }
-
- // If it's a directory, then make sure.
- if dirPath && !isDir {
- return syserror.ENOTDIR
- }
-
- // Don't allow directories to be opened writable.
- if isDir && fileFlags.Write {
- return syserror.EISDIR
+ if fs.IsDir(d.Inode.StableAttr) {
+ // Don't allow directories to be opened writable.
+ if fileFlags.Write {
+ return syserror.EISDIR
+ }
+ } else {
+ // If O_DIRECTORY is set, but the file is not a directory, then fail.
+ if fileFlags.Directory {
+ return syserror.ENOTDIR
+ }
+ // If it's a directory, then make sure.
+ if dirPath {
+ return syserror.ENOTDIR
+ }
+ if fileFlags.Write && flags&syscall.O_TRUNC != 0 {
+ if err := d.Inode.Truncate(t, d, 0); err != nil {
+ return err
+ }
+ }
}
file, err := d.Inode.GetFile(t, d, fileFlags)