Skip to contentSkip to conversation

O_CANLINK and flink

PATCH Ulrich Drepper 1 participant 1 message Dec 21, 2003 VFS

lore
Share & tools
lore
#0 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

This is a refresh of a patch I've sent quite some time ago (in April). That old patch introduced a flink() syscall without proper security measures. I've now integrated a proposal for how to fix it.

int flink (int fd, const char *newname)

The file associated with fd is linked with the newname. But this will only succeed if the file descriptor fd was created with the O_CANLINK flag set. It is not possible to set O_CANLINK afterwards, fcntl(F_SETFL) cannot set the bit, this is important.

The changes to implement this are pretty trivial. The patch consists of more than a few lines only because the link code is reusing as much as possible in the link() and flink() code and the O_* flags definitions were reformatted.

The purpose of this change is two fold.

For now it is possible to use this functionality in a couple of ways:

~ we can create quasi-anonymous files. Like

    fd = open ("RANDOM", O_EXCL|O_CREAT|O_CANLINK|O_RDWR, 0600);
    unlink ("RANDOM");
    ... do some work ...
    if (work is auccessful)
      flink (fd, "REALNAME");
    close (fd)

~ file descriptors which are passed to a process (by inheritance from the parent, through Unix sockets, ...) can be linked to the filesystem.

Longer-term I think the kernel should support real anonymous files which can optionally be created with the O_CANLINK flag. This would "only" save the unlink() call in the example above but not having the file at all in the filesystem namespace eliminates one more possible attack vector.

This patch covers so far only x86.

- --
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖ -----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/5T0n2ijCOnn/RHQRAmEOAJ9ZiAfMl0EcudUUREFki0Axpp/MLwCfc9qS HMTHoEkve1Tjc70jQTvdElw=
=InNQ
-----END PGP SIGNATURE-----

arch/i386/kernel/entry.S renamed from arch/i386/kernel/entry.S-canlink +1 -0
@@ -882,5 +882,6 @@
 	.long sys_utimes
  	.long sys_fadvise64_64
 	.long sys_ni_syscall	/* sys_vserver */
+	.long sys_flink
 
 syscall_table_size=(.-sys_call_table)
fs/namei.c renamed from fs/namei.c-canlink +36 -10

84 lines not on this page · raw message

include/asm-i386/fcntl.h renamed from include/asm-i386/fcntl.h-canlink +7 -6
@@ -14,12 +14,13 @@
 #define O_APPEND	  02000
 #define O_NONBLOCK	  04000
 #define O_NDELAY	O_NONBLOCK
-#define O_SYNC		 010000
-#define FASYNC		 020000	/* fcntl, for BSD compatibility */
-#define O_DIRECT	 040000	/* direct disk access hint */
-#define O_LARGEFILE	0100000
-#define O_DIRECTORY	0200000	/* must be a directory */
-#define O_NOFOLLOW	0400000 /* don't follow links */
+#define O_SYNC		  010000
+#define FASYNC		  020000	/* fcntl, for BSD compatibility */
+#define O_DIRECT	  040000	/* direct disk access hint */
+#define O_LARGEFILE	 0100000
+#define O_DIRECTORY	 0200000	/* must be a directory */
+#define O_NOFOLLOW	 0400000	/* don't follow links */
+#define O_CANLINK	01000000	/* flink can be used */
 
 #define F_DUPFD		0	/* dup */
 #define F_GETFD		1	/* get close_on_exec */
include/asm-i386/unistd.h renamed from include/asm-i386/unistd.h-canlink +2 -1
@@ -279,8 +279,9 @@
 #define __NR_utimes		271
 #define __NR_fadvise64_64	272
 #define __NR_vserver		273
+#define __NR_flink		274
 
-#define NR_syscalls 274
+#define NR_syscalls 275
 
 /* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */