PSARC/2009/284 faccessat(2) - determine accessibility of a file using file descriptors
authorSumanth Naropanth <Sumanth.Naropanth@Sun.COM>
Tue, 16 Jun 2009 11:36:35 -0700
changeset 9880 09668b3e273e
parent 9879 2547a76dc28b
child 9881 741c9e4e094c
PSARC/2009/284 faccessat(2) - determine accessibility of a file using file descriptors 6536147 accessat() should become a public interface after an agreement is reached on the function name
usr/src/cmd/auditrecord/audit_record_attr.txt
usr/src/cmd/rm/rm.c
usr/src/cmd/truss/systable.c
usr/src/head/unistd.h
usr/src/lib/libbsm/audit_event.txt
usr/src/lib/libc/amd64/Makefile
usr/src/lib/libc/i386/Makefile.com
usr/src/lib/libc/port/llib-lc
usr/src/lib/libc/port/mapfile-vers
usr/src/lib/libc/port/sys/faccessat.c
usr/src/lib/libc/port/sys/fsmisc.c
usr/src/lib/libc/sparc/Makefile.com
usr/src/lib/libc/sparcv9/Makefile.com
usr/src/uts/common/c2/audit_event.c
usr/src/uts/common/c2/audit_kevents.h
usr/src/uts/common/sys/fcntl.h
usr/src/uts/common/sys/syscall.h
usr/src/uts/common/syscall/access.c
usr/src/uts/common/syscall/fsat.c
--- a/usr/src/cmd/auditrecord/audit_record_attr.txt	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/cmd/auditrecord/audit_record_attr.txt	Tue Jun 16 11:36:35 2009 -0700
@@ -718,6 +718,10 @@
 label=AUE_EXPORTFS
   skip=Not used.
 
+label=AUE_FACCESSAT
+  see=access(2)
+  format=path:[path_attr]:[attr]
+
 label=AUE_FACLSET
   syscall=facl
   case=Invalid file descriptor
@@ -838,6 +842,7 @@
 #unlinkat	AUE_UNLINKAT
 #futimesat	AUE_FUTIMESAT
 #renameat	AUE_RENAMEAT
+#faccessat	AUE_FACCESSAT
 
 label=AUE_FSTAT
   skip=Not used.
--- a/usr/src/cmd/rm/rm.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/cmd/rm/rm.c	Tue Jun 16 11:36:35 2009 -0700
@@ -20,15 +20,13 @@
  */
 
 /*
- * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
 /*	All Rights Reserved   */
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 /*
  * rm [-fiRr] file ...
  */
@@ -49,8 +47,6 @@
 #include <values.h>
 #include "getresponse.h"
 
-#define	E_OK	010		/* make __accessat() use effective ids */
-
 #define	DIR_CANTCLOSE		1
 
 static struct stat rootdir;
@@ -87,8 +83,6 @@
 static int maxfds = MAXINT;
 static int nfds;
 
-extern int __accessat(int, const char *, int);
-
 int
 main(int argc, char **argv)
 {
@@ -388,7 +382,7 @@
 		 * prompt user for response.
 		 */
 		if (ontty && !interactive && !silent &&
-		    __accessat(caller->fd, entry, W_OK|X_OK|E_OK) != 0 &&
+		    faccessat(caller->fd, entry, W_OK|X_OK, AT_EACCESS) != 0 &&
 		    !confirm(stderr,
 		    gettext("rm: examine files in directory %s (%s/%s)? "),
 		    pathbuf, yesstr, nostr)) {
@@ -521,7 +515,7 @@
 		 *
 		 */
 		if (ontty && !S_ISLNK(temp.st_mode) &&
-		    __accessat(caller->fd, entry, W_OK|E_OK) != 0 &&
+		    faccessat(caller->fd, entry, W_OK, AT_EACCESS) != 0 &&
 		    !confirm(stdout,
 		    gettext("rm: %s: override protection %o (%s/%s)? "),
 		    pathbuf, temp.st_mode & 0777, yesstr, nostr)) {
--- a/usr/src/cmd/truss/systable.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/cmd/truss/systable.c	Tue Jun 16 11:36:35 2009 -0700
@@ -717,7 +717,7 @@
 {"unlinkat",	4, DEC, NOV, HID, ATC, STG, HEX},		/* 5 */
 {"futimesat",	4, DEC, NOV, HID, ATC, STG, HEX},		/* 6 */
 {"renameat",	5, DEC, NOV, HID, ATC, STG, DEC, STG},		/* 7 */
-{"__accessat",	5, DEC, NOV, HID, ATC, STG, ACC},		/* 8 */
+{"faccessat",	5, DEC, NOV, HID, ATC, STG, ACC, ACC},		/* 8 */
 {"__openattrdirat", 3, DEC, NOV, HID, ATC, STG},		/* 9 */
 {"openat",	4, DEC, NOV, HID, ATC, STG, OPN},		/* N - 2 */
 {"openat64",	4, DEC, NOV, HID, ATC, STG, OPN},		/* N - 1 */
@@ -953,7 +953,7 @@
 	{ "unlinkat",		SYS_fsat	},
 	{ "futimesat",		SYS_fsat	},
 	{ "renameat",		SYS_fsat	},
-	{ "__accessat",		SYS_fsat	},
+	{ "faccessat",		SYS_fsat	},
 	{ "__openattrdirat",	SYS_fsat	},
 	{ "lgrpsys",		SYS_lgrpsys	},
 	{ "getrusage",		SYS_rusagesys	},
--- a/usr/src/head/unistd.h	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/head/unistd.h	Tue Jun 16 11:36:35 2009 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -30,8 +30,6 @@
 #ifndef _UNISTD_H
 #define	_UNISTD_H
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 #include <sys/feature_tests.h>
 
 #include <sys/types.h>
@@ -560,6 +558,10 @@
 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
 extern void yield(void);
 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
+#if !defined(__XOPEN_OR_POSIX) || defined(_ATFILE_SOURCE) || \
+	defined(__EXTENSIONS__)
+extern int faccessat(int, const char *, int, int);
+#endif /* !defined(__XOPEN_OR_POSIX) || defined(_ATFILE_SOURCE)... */
 
 /* transitional large file interface versions */
 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
@@ -857,6 +859,10 @@
 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
 extern void yield();
 #endif /* !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__) */
+#if !defined(__XOPEN_OR_POSIX) || defined(_ATFILE_SOURCE) || \
+	defined(__EXTENSIONS__)
+extern int faccessat();
+#endif /* !defined(__XOPEN_OR_POSIX) || defined(_ATFILE_SOURCE)... */
 
 /* transitional large file interface versions */
 #if	defined(_LARGEFILE64_SOURCE) && !((_FILE_OFFSET_BITS == 64) && \
--- a/usr/src/lib/libbsm/audit_event.txt	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libbsm/audit_event.txt	Tue Jun 16 11:36:35 2009 -0700
@@ -355,6 +355,7 @@
 #
 307:AUE_SETSID:setsid(2):pm
 308:AUE_SETPGID:setpgid(2):pm
+309:AUE_FACCESSAT:faccessat(2):fa
 #
 # user level audit events
 #	2048 -  6143	Reserved
--- a/usr/src/lib/libc/amd64/Makefile	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/amd64/Makefile	Tue Jun 16 11:36:35 2009 -0700
@@ -795,7 +795,6 @@
 	execl.o			\
 	execle.o		\
 	execv.o			\
-	faccessat.o		\
 	fsmisc.o		\
 	fstatat.o		\
 	getpagesizes.o		\
--- a/usr/src/lib/libc/i386/Makefile.com	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/i386/Makefile.com	Tue Jun 16 11:36:35 2009 -0700
@@ -836,7 +836,6 @@
 	execl.o			\
 	execle.o		\
 	execv.o			\
-	faccessat.o		\
 	fsmisc.o		\
 	fstatat.o		\
 	getpagesizes.o		\
--- a/usr/src/lib/libc/port/llib-lc	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/port/llib-lc	Tue Jun 16 11:36:35 2009 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -1749,9 +1749,6 @@
 /* label.c */
 extern int is_system_labeled(void);
 
-/* until TOG resolves the name, keep this private */
-int __accessat(int, const char *, int);
-
 extern int uconv_u16tou32(const uint16_t *, size_t *, uint32_t *, size_t *,
 	int);
 extern int uconv_u16tou8(const uint16_t *, size_t *, uchar_t *, size_t *, int);
--- a/usr/src/lib/libc/port/mapfile-vers	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/port/mapfile-vers	Tue Jun 16 11:36:35 2009 -0700
@@ -101,6 +101,7 @@
 	door_unbind;
 	err;
 	errx;
+	faccessat;
 	fdatasync;
 	fgetattr;
 	forkallx;
@@ -1402,7 +1403,6 @@
 	_uberdata;
 	__xpg6 = NODIRECT;
     protected:
-	__accessat;
 	acctctl;
 	allocids;
 	_assert_c99;
--- a/usr/src/lib/libc/port/sys/faccessat.c	Wed Jun 17 02:10:30 2009 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-/*
- * CDDL HEADER START
- *
- * The contents of this file are subject to the terms of the
- * Common Development and Distribution License (the "License").
- * You may not use this file except in compliance with the License.
- *
- * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
- * or http://www.opensolaris.org/os/licensing.
- * See the License for the specific language governing permissions
- * and limitations under the License.
- *
- * When distributing Covered Code, include this CDDL HEADER in each
- * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
- * If applicable, add the following below this CDDL HEADER, with the
- * fields enclosed by brackets "[]" replaced with your own identifying
- * information: Portions Copyright [yyyy] [name of copyright owner]
- *
- * CDDL HEADER END
- */
-
-/*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
- */
-
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
-#include "lint.h"
-#include <sys/types.h>
-#include <sys/syscall.h>
-
-int
-__accessat(int fd, const char *name, int amode)
-{
-	return (syscall(SYS_fsat, 8, fd, name, amode));
-}
--- a/usr/src/lib/libc/port/sys/fsmisc.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/port/sys/fsmisc.c	Tue Jun 16 11:36:35 2009 -0700
@@ -20,12 +20,10 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 #pragma weak _fchownat = fchownat
 #pragma weak _futimesat = futimesat
 #pragma weak _unlinkat = unlinkat
@@ -61,6 +59,12 @@
 }
 
 int
+faccessat(int fd, const char *fname, int amode, int flag)
+{
+	return (syscall(SYS_fsat, 8, fd, fname, amode, flag));
+}
+
+int
 __openattrdirat(int fd, const char *name)
 {
 	return (syscall(SYS_fsat, 9, fd, name));
--- a/usr/src/lib/libc/sparc/Makefile.com	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/sparc/Makefile.com	Tue Jun 16 11:36:35 2009 -0700
@@ -854,7 +854,6 @@
 	execl.o			\
 	execle.o		\
 	execv.o			\
-	faccessat.o		\
 	fsmisc.o		\
 	fstatat.o		\
 	getpagesizes.o		\
--- a/usr/src/lib/libc/sparcv9/Makefile.com	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/lib/libc/sparcv9/Makefile.com	Tue Jun 16 11:36:35 2009 -0700
@@ -798,7 +798,6 @@
 	execl.o			\
 	execle.o		\
 	execv.o			\
-	faccessat.o		\
 	fsmisc.o		\
 	fstatat.o		\
 	getpagesizes.o		\
--- a/usr/src/uts/common/c2/audit_event.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/c2/audit_event.c	Tue Jun 16 11:36:35 2009 -0700
@@ -1024,6 +1024,9 @@
 	case 7: /* renameat */
 		e = AUE_RENAMEAT;
 		break;
+	case 8: /* faccessat */
+		e = AUE_FACCESSAT;
+		break;
 	case 9: /* __openattrdirat */
 		tad->tad_ctrl |= PAD_SAVPATH;
 		/*FALLTHROUGH*/
--- a/usr/src/uts/common/c2/audit_kevents.h	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/c2/audit_kevents.h	Tue Jun 16 11:36:35 2009 -0700
@@ -342,12 +342,13 @@
 #define	AUE_PORTFS_DISSOCIATE	306	/* =fa portfs(2) - port disassociate */
 #define	AUE_SETSID		307	/* =pm setsid(2) */
 #define	AUE_SETPGID		308	/* =pm setpgid(2) */
+#define	AUE_FACCESSAT		309	/* =fa faccessat(2) */
 
 
 
 /* NOTE: update MAX_KEVENTS below if events are added. */
 
-#define	MAX_KEVENTS		308
+#define	MAX_KEVENTS		309
 
 
 #ifdef __cplusplus
--- a/usr/src/uts/common/sys/fcntl.h	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/sys/fcntl.h	Tue Jun 16 11:36:35 2009 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -40,8 +40,6 @@
 #ifndef	_SYS_FCNTL_H
 #define	_SYS_FCNTL_H
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 #include <sys/feature_tests.h>
 
 #include <sys/types.h>
@@ -351,6 +349,7 @@
 #define	AT_SYMLINK_NOFOLLOW		0x1000
 #define	AT_REMOVEDIR			0x1
 #define	_AT_TRIGGER			0x2
+#define	AT_EACCESS			0x4	/* use EUID/EGID for access */
 #endif
 
 #if !defined(__XOPEN_OR_POSIX) || defined(_XPG6) || defined(__EXTENSIONS__)
--- a/usr/src/uts/common/sys/syscall.h	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/sys/syscall.h	Tue Jun 16 11:36:35 2009 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -222,6 +222,7 @@
 	 *	fchownat(...)	:: fsat(5, ...)
 	 *	unlinkat(...)	:: fsat(6, ...)
 	 *	futimesat(...)	:: fsat(7, ...)
+	 *	faccessat(...)	:: fsat(8, ...)
 	 */
 #define	SYS_lwp_park	77
 	/*
--- a/usr/src/uts/common/syscall/access.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/syscall/access.c	Tue Jun 16 11:36:35 2009 -0700
@@ -46,6 +46,7 @@
 #include <sys/file.h>
 #include <fs/fs_subr.h>
 #include <c2/audit.h>
+#include <sys/fcntl.h>
 
 /*
  * Determine accessibility of file.
@@ -123,7 +124,7 @@
 }
 
 int
-accessat(int fd, char *fname, int fmode)
+faccessat(int fd, char *fname, int fmode, int flag)
 {
 	file_t *dirfp;
 	vnode_t *dirvp;
@@ -133,6 +134,9 @@
 	if (fd == AT_FDCWD && fname == NULL)
 		return (set_errno(EFAULT));
 
+	if ((flag & ~AT_EACCESS) != 0)
+		return (set_errno(EINVAL));
+
 	if (fname != NULL) {
 		if (copyin(fname, &startchar, sizeof (char)))
 			return (set_errno(EFAULT));
@@ -157,6 +161,11 @@
 	if (audit_active)
 		audit_setfsat_path(1);
 
+	/* Do not allow E_OK unless AT_EACCESS flag is set */
+	fmode &= ~E_OK;
+	if (flag & AT_EACCESS)
+		fmode |= E_OK;
+
 	error = caccess(fname, fmode, dirvp);
 	if (dirvp != NULL)
 		VN_RELE(dirvp);
--- a/usr/src/uts/common/syscall/fsat.c	Wed Jun 17 02:10:30 2009 -0700
+++ b/usr/src/uts/common/syscall/fsat.c	Tue Jun 16 11:36:35 2009 -0700
@@ -19,12 +19,10 @@
  * CDDL HEADER END
  */
 /*
- * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
-#pragma ident	"%Z%%M%	%I%	%E% SMI"
-
 #include <sys/types.h>
 #include <sys/errno.h>
 #include <sys/fcntl.h>
@@ -41,7 +39,7 @@
 extern int fchownat(int, char *, uid_t, gid_t, int);
 extern int fstatat(int, char *, struct stat *, int);
 extern int futimesat(int, char *, struct timeval *);
-extern int accessat(int, char *, int);
+extern int faccessat(int, char *, int, int);
 extern int openattrdirat(int, char *);
 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
 extern int fstatat64_32(int, char *, struct stat64_32 *, int);
@@ -65,7 +63,7 @@
  * 5 - unlinkat
  * 6 - futimesat
  * 7 - renameat
- * 8 - accessat
+ * 8 - faccessat
  * 9 - openattrdirat
  *
  * The code for handling the at functionality exists in the file where the
@@ -118,8 +116,9 @@
 	case 7: /* renameat */
 		return (renameat((int)arg1, (char *)arg2, (int)arg3,
 		    (char *)arg4));
-	case 8: /* accessat */
-		return (accessat((int)arg1, (char *)arg2, (int)arg3));
+	case 8: /* faccessat */
+		return (faccessat((int)arg1, (char *)arg2, (int)arg3,
+		    (int)arg4));
 	case 9: /* openattrdirat */
 		return (openattrdirat((int)arg1, (char *)arg2));
 	default:
@@ -162,8 +161,9 @@
 	case 7: /* renameat */
 		return (renameat((int)arg1, (char *)arg2, (int)arg3,
 		    (char *)arg4));
-	case 8: /* accessat */
-		return (accessat((int)arg1, (char *)arg2, (int)arg3));
+	case 8: /* faccessat */
+		return (faccessat((int)arg1, (char *)arg2, (int)arg3,
+		    (int)arg4));
 	case 9: /* openattrdirat */
 		return (openattrdirat((int)arg1, (char *)arg2));
 	default: