• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2015 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author:
19  * Alexey Kodanev <alexey.kodanev@oracle.com>
20  *
21  * Test checks following preconditions:
22  * since Linux kernel 3.7 it is possible to set extended attributes
23  * to cgroup files.
24  */
25 
26 #include <sys/stat.h>
27 #include <sys/mount.h>
28 #include <sys/types.h>
29 #include <sys/xattr.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <dirent.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 
38 #include "test.h"
39 #include "safe_macros.h"
40 
41 char *TCID = "cgroup_xattr";
42 
43 static const char subdir_name[]	= "test";
44 
45 #define MAX_SUBSYS		16
46 #define MAX_OPTIONS_LEN		256
47 #define MAX_DIR_NAME		64
48 
49 /* struct to store available mount options */
50 struct cgrp_option {
51 	char str[MAX_OPTIONS_LEN];
52 	char dir[MAX_DIR_NAME];
53 	int hier;
54 	int mounted;
55 	int subdir;
56 };
57 static struct cgrp_option cgrp_opt[MAX_SUBSYS];
58 static int cgrp_opt_num;
59 
60 struct tst_key {
61 	const char *name;
62 	int good;
63 };
64 
65 /* only security.* & trusted.* are valid key names */
66 static struct tst_key tkeys[] = {
67 	{ .name = "security.",		.good = 0,	},  /* see setup() */
68 	{ .name = "trusted.test",	.good = 1,	},
69 	{ .name = "trusted.",		.good = 0,	},  /* see setup() */
70 	{ .name = "user.",		.good = 0,	},
71 	{ .name = "system.",		.good = 0,	},
72 };
73 
74 #define DEFAULT_VALUE_SIZE	8
75 
76 /* struct to store key's value */
77 struct tst_val {
78 	char *buf;
79 	size_t size;
80 };
81 static struct tst_val val;
82 
83 /* it fills value's buffer */
84 static char id;
85 
86 /*
87  * When test breaks, all open dirs should be closed
88  * otherwise umount won't succeed
89  */
90 #define MAX_OPEN_DIR		32
91 static DIR *odir[MAX_OPEN_DIR];
92 static int odir_num;
93 
94 /* test options */
95 static char *narg;
96 static int nflag;
97 static int skip_cleanup;
98 static int verbose;
99 static const option_t options[] = {
100 	{"n:", &nflag, &narg},
101 	{"s", &skip_cleanup, NULL},
102 	{"v", &verbose, NULL},
103 	{NULL, NULL, NULL}
104 };
105 
106 static void help(void);
107 static void setup(int argc, char *argv[]);
108 static void test_run(void);
109 static void cleanup(void);
110 
111 static int mount_cgroup(void);
112 static int set_xattrs(const char *file);
113 static int get_xattrs(const char *file);
114 /*
115  * set or get xattr recursively
116  *
117  * @path: start directory
118  * @xattr_operation: can be set_xattrs() or get_xattrs()
119  */
120 static int cgrp_files_walking(const char *path,
121 	int (*xattr_operation)(const char *));
122 
main(int argc,char * argv[])123 int main(int argc, char *argv[])
124 {
125 	setup(argc, argv);
126 
127 	test_run();
128 
129 	cleanup();
130 
131 	tst_exit();
132 }
133 
help(void)134 static void help(void)
135 {
136 	printf("  -n x    Write x bytes to xattr value, default is %d\n",
137 		DEFAULT_VALUE_SIZE);
138 	printf("  -s      Skip cleanup\n");
139 	printf("  -v      Verbose\n");
140 }
141 
setup(int argc,char * argv[])142 void setup(int argc, char *argv[])
143 {
144 	unsigned int i;
145 
146 	tst_parse_opts(argc, argv, options, help);
147 
148 	tst_require_root();
149 
150 	if (access("/proc/cgroups", F_OK) == -1)
151 		tst_brkm(TCONF, NULL, "Kernel doesn't support cgroups");
152 
153 	for (i = 0; i < ARRAY_SIZE(tkeys); ++i) {
154 		if (!strcmp(tkeys[i].name, "trusted."))
155 			tkeys[i].good = tst_kvercmp(4, 5, 0) < 0;
156 	}
157 
158 	int value_size = DEFAULT_VALUE_SIZE;
159 	if (nflag) {
160 		if (sscanf(narg, "%i", &value_size) != 1)
161 			tst_brkm(TBROK, NULL, "-n option arg is not a number");
162 		if (value_size <= 0)
163 			tst_brkm(TBROK, NULL, "-n option arg is less than 1");
164 	}
165 
166 	/* initialize test value */
167 	val.size = value_size;
168 	val.buf = SAFE_MALLOC(NULL, value_size);
169 
170 	tst_sig(FORK, DEF_HANDLER, cleanup);
171 
172 	tst_tmpdir();
173 
174 	if (!mount_cgroup())
175 		tst_brkm(TCONF, cleanup, "Nothing  mounted");
176 }
177 
test_run(void)178 static void test_run(void)
179 {
180 	int i, set_res = 0, get_res = 0;
181 
182 	for (i = 0; i < cgrp_opt_num; ++i) {
183 		if (!cgrp_opt[i].mounted)
184 			continue;
185 
186 		SAFE_CHDIR(cleanup, cgrp_opt[i].dir);
187 		/* reset value */
188 		id = 0;
189 		/* set xattr to each file in cgroup fs */
190 		set_res |= cgrp_files_walking(".", set_xattrs);
191 
192 		id = 0;
193 		/* get & check xattr */
194 		get_res |= cgrp_files_walking(".", get_xattrs);
195 		SAFE_CHDIR(cleanup, "..");
196 	}
197 
198 	/* final results */
199 	tst_resm(TINFO, "All test-cases have been completed, summary:");
200 	tst_resm(TINFO, "Set tests result: %s", (set_res) ? "FAIL" : "PASS");
201 	tst_resm(TINFO, "Get tests result: %s", (get_res) ? "FAIL" : "PASS");
202 }
203 
cleanup(void)204 static void cleanup(void)
205 {
206 	if (val.buf != NULL)
207 		free(val.buf);
208 
209 	if (skip_cleanup)
210 		return;
211 
212 	/*
213 	 * Kernels 3.7 can crash while unmounting cgroups with xattr,
214 	 * call tst_old_flush() to make sure all buffered data written
215 	 * before it happens
216 	 */
217 	tst_old_flush();
218 
219 	int i;
220 	for (i = 0; i < odir_num; ++i) {
221 		SAFE_CLOSEDIR(NULL, odir[i]);
222 	}
223 
224 	char *cwd = tst_get_tmpdir();
225 	SAFE_CHDIR(NULL, cwd);
226 	free(cwd);
227 
228 	for (i = 0; i < cgrp_opt_num; ++i) {
229 		if (cgrp_opt[i].subdir) {
230 			SAFE_CHDIR(NULL, cgrp_opt[i].dir);
231 			SAFE_RMDIR(NULL, subdir_name);
232 			SAFE_CHDIR(NULL, "..");
233 		}
234 		if (cgrp_opt[i].mounted) {
235 			SAFE_UMOUNT(NULL, cgrp_opt[i].dir);
236 		}
237 	}
238 
239 	tst_rmdir();
240 }
241 
mount_cgroup(void)242 int mount_cgroup(void)
243 {
244 	FILE *fd = fopen("/proc/cgroups", "r");
245 	if (fd == NULL)
246 		tst_brkm(TBROK, cleanup, "Failed to read /proc/cgroups");
247 	char str[MAX_DIR_NAME], name[MAX_DIR_NAME];
248 	int hier = 0, num = 0, enabled = 0, first = 1;
249 	/* make mount options */
250 	while ((fgets(str, MAX_DIR_NAME, fd)) != NULL) {
251 		/* skip first line */
252 		if (first) {
253 			first = 0;
254 			continue;
255 		}
256 		if (sscanf(str, "%s\t%d\t%d\t%d",
257 			name, &hier, &num, &enabled) != 4)
258 			tst_brkm(TBROK, cleanup, "Can't parse /proc/cgroups");
259 		if (!enabled)
260 			continue;
261 
262 		/* BUG WORKAROUND
263 		 * Only mount those subsystems, which are not mounted yet.
264 		 * It's a workaround to a bug when mount doesn't return any err
265 		 * code while mounting already mounted subsystems, but with
266 		 * additional "xattr" option. In that case, mount will succeed,
267 		 * but xattr won't be supported in the new mount anyway.
268 		 * Should be removed as soon as a fix committed to upstream.
269 		 */
270 
271 		int i, found = 0;
272 		for (i = 0; i < cgrp_opt_num; ++i) {
273 			if (cgrp_opt[i].hier == hier) {
274 				found = 1;
275 				break;
276 			}
277 		}
278 		if (!found) {
279 			i = cgrp_opt_num++;
280 			cgrp_opt[i].hier = hier;
281 		}
282 		char *str = cgrp_opt[i].str;
283 		if (str[0] == '\0')
284 			strcpy(str, "xattr");
285 		snprintf(str + strlen(str), MAX_OPTIONS_LEN - strlen(str),
286 			",%s", name);
287 	}
288 	fclose(fd);
289 
290 	int i, any_mounted = 0;
291 	for (i = 0; i < cgrp_opt_num; ++i) {
292 		char dir[MAX_DIR_NAME];
293 		struct cgrp_option *opt = &cgrp_opt[i];
294 		tst_resm(TINFO, "mount options %d: %s (hier = %d)",
295 			i, opt->str, opt->hier);
296 		snprintf(opt->dir, MAX_DIR_NAME, "cgx_%d", opt->hier);
297 		SAFE_MKDIR(cleanup, opt->dir, 0755);
298 
299 		if (mount(opt->dir, opt->dir, "cgroup", 0, opt->str) == -1) {
300 			tst_resm(TINFO, "Can't mount: %s", dir);
301 			continue;
302 		}
303 
304 		any_mounted = 1;
305 		opt->mounted = 1;
306 
307 		/* create new hierarchy */
308 		SAFE_CHDIR(cleanup, opt->dir);
309 		SAFE_MKDIR(cleanup, subdir_name, 0755);
310 		opt->subdir = 1;
311 		SAFE_CHDIR(cleanup, "..");
312 	}
313 	return any_mounted;
314 }
315 
set_xattrs(const char * file)316 static int set_xattrs(const char *file)
317 {
318 	unsigned int i, err, fail, res = 0;
319 
320 	for (i = 0; i < ARRAY_SIZE(tkeys); ++i) {
321 		err = setxattr(file, tkeys[i].name,
322 			(const void *)val.buf, val.size, 0) == -1;
323 
324 		fail = err && tkeys[i].good;
325 		res |= fail;
326 
327 		tst_resm((fail) ? TFAIL : TPASS,
328 			"Expect: %s set xattr key '%s' to file '%s'",
329 			(tkeys[i].good) ? "can" : "can't",
330 			tkeys[i].name, file);
331 
332 		if (verbose && tkeys[i].good)
333 			tst_resm_hexd(TINFO, val.buf, val.size, "value:");
334 	}
335 	return res;
336 }
337 
get_xattrs(const char * file)338 static int get_xattrs(const char *file)
339 {
340 	unsigned int i, fail, res = 0;
341 
342 	for (i = 0; i < ARRAY_SIZE(tkeys); ++i) {
343 		/* get value size */
344 		ssize_t size = getxattr(file, tkeys[i].name, NULL, 0);
345 		fail = (size == -1 && tkeys[i].good);
346 		res |= fail;
347 
348 		tst_resm((fail) ? TFAIL : TPASS,
349 			"Expect: %s read xattr %s of file '%s'",
350 			(tkeys[i].good) ? "can" : "can't",
351 			tkeys[i].name, file);
352 
353 		if (fail || size == -1)
354 			continue;
355 
356 		/* get xattr value */
357 		char xval[size];
358 		if (getxattr(file, tkeys[i].name, xval, size) == -1) {
359 			tst_brkm(TBROK, cleanup,
360 				"Can't get buffer of key %s",
361 				tkeys[i].name);
362 		}
363 		fail = val.size != (size_t)size ||
364 			strncmp(val.buf, xval, val.size) != 0;
365 		res |= fail;
366 
367 		tst_resm((fail) ? TFAIL : TPASS, "Expect: values equal");
368 
369 		if (verbose && fail) {
370 			tst_resm_hexd(TINFO, xval, size,
371 				"Read  xattr  value:");
372 			tst_resm_hexd(TINFO, val.buf, val.size,
373 				"Expect xattr value:");
374 		}
375 	}
376 	return res;
377 }
378 
cgrp_files_walking(const char * path,int (* xattr_operation)(const char *))379 static int cgrp_files_walking(const char *path,
380 	int (*xattr_operation)(const char *))
381 {
382 	int res = 0;
383 	struct dirent *entry;
384 	DIR *dir = opendir(path);
385 
386 	odir[odir_num] = dir;
387 	if (++odir_num >= MAX_OPEN_DIR) {
388 		tst_brkm(TBROK, cleanup,
389 			"Unexpected num of open dirs, max: %d", MAX_OPEN_DIR);
390 	}
391 
392 	SAFE_CHDIR(cleanup, path);
393 
394 	tst_resm(TINFO, "In dir %s", path);
395 
396 	errno = 0;
397 	while ((entry = readdir(dir)) != NULL) {
398 		const char *file = entry->d_name;
399 		/* skip current and up directories */
400 		if (!strcmp(file, "..") || !strcmp(file, "."))
401 			continue;
402 		struct stat stat_buf;
403 		TEST(lstat(file, &stat_buf));
404 		if (TEST_RETURN != -1 && S_ISDIR(stat_buf.st_mode)) {
405 			/* proceed to subdir */
406 			res |= cgrp_files_walking(file, xattr_operation);
407 			tst_resm(TINFO, "In dir %s", path);
408 		}
409 		memset(val.buf, id++, val.size);
410 		res |= xattr_operation(file);
411 		errno = 0;
412 	}
413 	if (errno && !entry) {
414 		tst_brkm(TWARN | TERRNO, cleanup,
415 			"Error while reading dir '%s'", path);
416 	}
417 	if (closedir(dir) == -1)
418 		tst_brkm(TWARN, cleanup, "Failed to close dir '%s'", path);
419 	else
420 		odir[--odir_num] = NULL;
421 
422 	if (strcmp(path, "."))
423 		SAFE_CHDIR(cleanup, "..");
424 	return res;
425 }
426