xref: /freebsd/tools/regression/priv/priv_vfs_extattr_system.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*-
2  * Copyright (c) 2006 nCircle Network Security, Inc.
3  * All rights reserved.
4  *
5  * This software was developed by Robert N. M. Watson for the TrustedBSD
6  * Project under contract to nCircle Network Security, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY,
21  * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 /*
33  * Test that privilege is required to write to the system extended attribute
34  * namespace.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/extattr.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "main.h"
46 
47 #define	EA_NAMESPACE	EXTATTR_NAMESPACE_SYSTEM
48 #define	EA_NAME		"test"
49 #define	EA_DATA		"test"
50 #define	EA_SIZE		strlen(EA_DATA)
51 
52 void
53 priv_vfs_extattr_system(void)
54 {
55 	char fpath[1024];
56 	int error;
57 
58 	assert_root();
59 
60 	/*
61 	 * Set file perms so that discretionary access control would grant
62 	 * write rights on non-system EAs on the file.
63 	 */
64 	setup_file(fpath, UID_ROOT, GID_WHEEL, 0666);
65 
66 	/*
67 	 * Try with privilege.
68 	 */
69 	if (extattr_set_file(fpath, EA_NAMESPACE, EA_NAME, EA_DATA, EA_SIZE)
70 	    < 0) {
71 		warn("extattr_set_file(SYSTEM, %s, %s, %d) as root",
72 		    EA_NAME, EA_DATA, EA_SIZE);
73 		goto out;
74 	}
75 
76 	set_euid(UID_OTHER);
77 
78 	/*
79 	 * Try without privilege.
80 	 */
81 	error = extattr_set_file(fpath, EA_NAMESPACE, EA_NAME, EA_DATA,
82 	   EA_SIZE);
83 	if (error == 0) {
84 		warn("extattr_set_file(SYSTEM, %s, %s, %d) succeeded as !root",
85 		    EA_NAME, EA_DATA, EA_SIZE);
86 		goto out;
87 	}
88 	if (errno != EPERM) {
89 		warn("extattr_set_file(SYSTEM, %s, %s, %d) wrong errno %d "
90 		    "as !root", EA_NAME, EA_DATA, EA_SIZE, errno);
91 		goto out;
92 	}
93 out:
94 	seteuid(UID_ROOT);
95 	(void)unlink(fpath);
96 }
97