Re: fcntl(2) and other file systems like XFS

From: Jamie Lokier (lk@tantalophile.demon.co.uk)
Date: Wed Mar 22 2000 - 09:11:57 EST


Linus Torvalds wrote:
> Do a
>
> #define O_NONE 3
>
> and see if it still works.. I suspect it does - I was careful to have
> open() change the flags by doing something like
>
> flags = (flags + 1) & 3;
>
> which turns the O_ACCMODE bits into two nice bits (bit #0 is "readable"
> and bit #1 is "writable" instead of the standard O_ACCMODE braindamage).

[Aside: What is the purpose of the next line?
        if (f->f_mode)
           flag++;
]

O_NONE nearly works, just the permission check at the start is broken:

        #include <fcntl.h>
        #include <sys/stat.h>
        #define O_NONE 3
        int main (int argc, char ** argv)
        {
          int fd = open (argv [1], O_NONE);
          struct stat st;
          read (fd, 0, 1);
          write (fd, 0, 1);
          fstat (fd, &st);
          fchdir (fd);
          fchown (fd, 0);
          fchmod (fd, 0777);
          close (fd);
        }

On a file with permission 600:

open("f", 0x4) = 3
read(3, 0, 1) = -1 EBADF (Bad file descriptor)
write(3, NULL, 1) = -1 EBADF (Bad file descriptor)
fstat(3, {st_mode=0, st_size=0, ...}) = 0
fchdir(3) = -1 ENOTDIR (Not a directory)
fchown(3, 0, 1) = -1 EPERM (Operation not
permitted)
fchmod(3, 0777) = 0
close(3) = 0

Ignore the fstat result -- it's working but strace is broken.

So fstat, fchdir, fchown and fchmod appear to be working, and read &
write are not permitted.

This is looking good!

On a file with permission 400 or 200 or 000:

open("f", 0x4) = -1 EACCES (Permission denied)

Oh dear, you need to read _and_ write permission to open this file with
mode O_NONE. And then you can't read or write it :-)

For completeness, a directory with mode 775:

open("tmp", 0x4) = -1 EISDIR (Is a directory)

Looks like the directory permissions check is confused too. The same
thing happens with O_DIRECTORY or a trailing slash.

I expect ioctls are not properly restricted for an O_NONE file, but I
didn't try. It wouldn't be hard to simply avoid opening the underlying
device -- thus allowing the fs-specific through to the fs.

That would fix the fs-specific flags thing once and for all, without
requiring new syscalls. And it would be more flexible.

-- Jamie

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Thu Mar 23 2000 - 21:00:36 EST