test: use consistent quoting
[coreutils.git] / src / selinux.c
blob6479c939269858c7338e4f8bed10873a48cf87d4
1 /* selinux - core functions for maintaining SELinux labeling
2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will 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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Daniel Walsh <dwalsh@redhat.com> */
19 #include <config.h>
20 #include <selinux/selinux.h>
21 #include <selinux/context.h>
22 #include <sys/types.h>
24 #include "error.h"
25 #include "system.h"
26 #include "canonicalize.h"
27 #include "dosname.h"
28 #include "xfts.h"
29 #include "selinux.h"
31 #if HAVE_SELINUX_SELINUX_H
33 # if ! HAVE_MODE_TO_SECURITY_CLASS
35 This function has been added to libselinux-2.1.12-5, but is here
36 for support with older versions of SELinux
38 Translates a mode into an Internal SELinux security_class definition.
39 Returns 0 on failure, with errno set to EINVAL.
41 static security_class_t
42 mode_to_security_class (mode_t m)
45 if (S_ISREG (m))
46 return string_to_security_class ("file");
47 if (S_ISDIR (m))
48 return string_to_security_class ("dir");
49 if (S_ISCHR (m))
50 return string_to_security_class ("chr_file");
51 if (S_ISBLK (m))
52 return string_to_security_class ("blk_file");
53 if (S_ISFIFO (m))
54 return string_to_security_class ("fifo_file");
55 if (S_ISLNK (m))
56 return string_to_security_class ("lnk_file");
57 if (S_ISSOCK (m))
58 return string_to_security_class ("sock_file");
60 errno = EINVAL;
61 return 0;
63 # endif
66 This function takes a PATH and a MODE and then asks SELinux what the label
67 of the path object would be if the current process label created it.
68 It then returns the label.
70 Returns -1 on failure. errno will be set appropriately.
73 static int
74 computecon (char const *path, mode_t mode, char **con)
76 char *scon = NULL;
77 char *tcon = NULL;
78 security_class_t tclass;
79 int rc = -1;
81 char *dir = dir_name (path);
82 if (!dir)
83 goto quit;
84 if (getcon (&scon) < 0)
85 goto quit;
86 if (getfilecon (dir, &tcon) < 0)
87 goto quit;
88 tclass = mode_to_security_class (mode);
89 if (!tclass)
90 goto quit;
91 rc = security_compute_create (scon, tcon, tclass, con);
93 quit:
94 free (dir);
95 freecon (scon);
96 freecon (tcon);
97 return rc;
101 This function takes a path and a mode, it calls computecon to get the
102 label of the path object if the current process created it, then it calls
103 matchpathcon to get the default type for the object. It substitutes the
104 default type into label. It tells the SELinux Kernel to label all new file
105 system objects created by the current process with this label.
107 Returns -1 on failure. errno will be set appropriately.
110 defaultcon (char const *path, mode_t mode)
112 int rc = -1;
113 char *scon = NULL;
114 char *tcon = NULL;
115 context_t scontext = 0, tcontext = 0;
116 const char *contype;
117 char *constr;
118 char *newpath = NULL;
120 if (! IS_ABSOLUTE_FILE_NAME (path))
122 /* Generate absolute path as required by subsequent matchpathcon(),
123 with libselinux < 2.1.5 2011-0826. */
124 newpath = canonicalize_filename_mode (path, CAN_MISSING);
125 if (! newpath)
126 error (EXIT_FAILURE, errno, _("error canonicalizing %s"),
127 quoteaf (path));
128 path = newpath;
131 if (matchpathcon (path, mode, &scon) < 0)
133 /* "No such file or directory" is a confusing error,
134 when processing files, when in fact it was the
135 associated default context that was not found.
136 Therefore map the error to something more appropriate
137 to the context in which we're using matchpathcon(). */
138 if (errno == ENOENT)
139 errno = ENODATA;
140 goto quit;
142 if (computecon (path, mode, &tcon) < 0)
143 goto quit;
144 if (!(scontext = context_new (scon)))
145 goto quit;
146 if (!(tcontext = context_new (tcon)))
147 goto quit;
149 if (!(contype = context_type_get (scontext)))
150 goto quit;
151 if (context_type_set (tcontext, contype))
152 goto quit;
153 if (!(constr = context_str (tcontext)))
154 goto quit;
156 rc = setfscreatecon (constr);
158 quit:
159 context_free (scontext);
160 context_free (tcontext);
161 freecon (scon);
162 freecon (tcon);
163 free (newpath);
164 return rc;
168 This function takes a PATH of an existing file system object, and a LOCAL
169 boolean that indicates whether the function should set the object's label
170 to the default for the local process, or one using system wide settings.
171 If LOCAL == true, it will ask the SELinux Kernel what the default label
172 for all objects created should be and then sets the label on the object.
173 Otherwise it calls matchpathcon on the object to ask the system what the
174 default label should be, extracts the type field and then modifies the file
175 system object. Note only the type field is updated, thus preserving MLS
176 levels and user identity etc. of the PATH.
178 Returns -1 on failure. errno will be set appropriately.
180 static int
181 restorecon_private (char const *path, bool local)
183 int rc = -1;
184 struct stat sb;
185 char *scon = NULL;
186 char *tcon = NULL;
187 context_t scontext = 0, tcontext = 0;
188 const char *contype;
189 char *constr;
190 int fd;
192 if (local)
194 if (getfscreatecon (&tcon) < 0)
195 return rc;
196 if (!tcon)
198 errno = ENODATA;
199 return rc;
201 rc = lsetfilecon (path, tcon);
202 freecon (tcon);
203 return rc;
206 fd = open (path, O_RDONLY | O_NOFOLLOW);
207 if (fd == -1 && (errno != ELOOP))
208 goto quit;
210 if (fd != -1)
212 if (fstat (fd, &sb) < 0)
213 goto quit;
215 else
217 if (lstat (path, &sb) < 0)
218 goto quit;
221 if (matchpathcon (path, sb.st_mode, &scon) < 0)
223 /* "No such file or directory" is a confusing error,
224 when processing files, when in fact it was the
225 associated default context that was not found.
226 Therefore map the error to something more appropriate
227 to the context in which we're using matchpathcon(). */
228 if (errno == ENOENT)
229 errno = ENODATA;
230 goto quit;
232 if (!(scontext = context_new (scon)))
233 goto quit;
235 if (fd != -1)
237 if (fgetfilecon (fd, &tcon) < 0)
238 goto quit;
240 else
242 if (lgetfilecon (path, &tcon) < 0)
243 goto quit;
246 if (!(tcontext = context_new (tcon)))
247 goto quit;
249 if (!(contype = context_type_get (scontext)))
250 goto quit;
251 if (context_type_set (tcontext, contype))
252 goto quit;
253 if (!(constr = context_str (tcontext)))
254 goto quit;
256 if (fd != -1)
257 rc = fsetfilecon (fd, constr);
258 else
259 rc = lsetfilecon (path, constr);
261 quit:
262 if (fd != -1)
263 close (fd);
264 context_free (scontext);
265 context_free (tcontext);
266 freecon (scon);
267 freecon (tcon);
268 return rc;
272 This function takes three parameters:
274 PATH of an existing file system object.
276 A RECURSE boolean which if the file system object is a directory, will
277 call restorecon_private on every file system object in the directory.
279 A LOCAL boolean that indicates whether the function should set object labels
280 to the default for the local process, or use system wide settings.
282 Returns false on failure. errno will be set appropriately.
284 bool
285 restorecon (char const *path, bool recurse, bool local)
287 char *newpath = NULL;
288 FTS *fts;
289 bool ok = true;
291 if (! IS_ABSOLUTE_FILE_NAME (path) && ! local)
293 /* Generate absolute path as required by subsequent matchpathcon(),
294 with libselinux < 2.1.5 2011-0826. Also generating the absolute
295 path before the fts walk, will generate absolute paths in the
296 fts entries, which may be quicker to process in any case. */
297 newpath = canonicalize_filename_mode (path, CAN_MISSING);
298 if (! newpath)
299 error (EXIT_FAILURE, errno, _("error canonicalizing %s"),
300 quoteaf (path));
303 const char *ftspath[2] = { newpath ? newpath : path, NULL };
305 if (! recurse)
307 ok = restorecon_private (*ftspath, local) != -1;
308 free (newpath);
309 return ok;
312 fts = xfts_open ((char *const *) ftspath, FTS_PHYSICAL, NULL);
313 while (1)
315 FTSENT *ent;
317 ent = fts_read (fts);
318 if (ent == NULL)
320 if (errno != 0)
322 error (0, errno, _("fts_read failed"));
323 ok = false;
325 break;
328 ok &= restorecon_private (fts->fts_path, local) != -1;
331 if (fts_close (fts) != 0)
333 error (0, errno, _("fts_close failed"));
334 ok = false;
337 free (newpath);
338 return ok;
340 #endif