maint: update all copyright year number ranges
[coreutils.git] / src / selinux.c
blobdd4c3ce6577616328dc7f8271677f0d0a915dde2
1 /* selinux - core functions for maintaining SELinux labeling
2 Copyright (C) 2012-2017 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 "die.h"
25 #include "error.h"
26 #include "system.h"
27 #include "canonicalize.h"
28 #include "dosname.h"
29 #include "xfts.h"
30 #include "selinux.h"
32 #if HAVE_SELINUX_SELINUX_H
34 # if ! HAVE_MODE_TO_SECURITY_CLASS
36 This function has been added to libselinux-2.1.12-5, but is here
37 for support with older versions of SELinux
39 Translates a mode into an Internal SELinux security_class definition.
40 Returns 0 on failure, with errno set to EINVAL.
42 static security_class_t
43 mode_to_security_class (mode_t m)
46 if (S_ISREG (m))
47 return string_to_security_class ("file");
48 if (S_ISDIR (m))
49 return string_to_security_class ("dir");
50 if (S_ISCHR (m))
51 return string_to_security_class ("chr_file");
52 if (S_ISBLK (m))
53 return string_to_security_class ("blk_file");
54 if (S_ISFIFO (m))
55 return string_to_security_class ("fifo_file");
56 if (S_ISLNK (m))
57 return string_to_security_class ("lnk_file");
58 if (S_ISSOCK (m))
59 return string_to_security_class ("sock_file");
61 errno = EINVAL;
62 return 0;
64 # endif
67 This function takes a PATH and a MODE and then asks SELinux what the label
68 of the path object would be if the current process label created it.
69 It then returns the label.
71 Returns -1 on failure. errno will be set appropriately.
74 static int
75 computecon (char const *path, mode_t mode, char **con)
77 char *scon = NULL;
78 char *tcon = NULL;
79 security_class_t tclass;
80 int rc = -1;
82 char *dir = dir_name (path);
83 if (!dir)
84 goto quit;
85 if (getcon (&scon) < 0)
86 goto quit;
87 if (getfilecon (dir, &tcon) < 0)
88 goto quit;
89 tclass = mode_to_security_class (mode);
90 if (!tclass)
91 goto quit;
92 rc = security_compute_create (scon, tcon, tclass, con);
94 quit:
95 free (dir);
96 freecon (scon);
97 freecon (tcon);
98 return rc;
102 This function takes a path and a mode, it calls computecon to get the
103 label of the path object if the current process created it, then it calls
104 matchpathcon to get the default type for the object. It substitutes the
105 default type into label. It tells the SELinux Kernel to label all new file
106 system objects created by the current process with this label.
108 Returns -1 on failure. errno will be set appropriately.
111 defaultcon (char const *path, mode_t mode)
113 int rc = -1;
114 char *scon = NULL;
115 char *tcon = NULL;
116 context_t scontext = 0, tcontext = 0;
117 const char *contype;
118 char *constr;
119 char *newpath = NULL;
121 if (! IS_ABSOLUTE_FILE_NAME (path))
123 /* Generate absolute path as required by subsequent matchpathcon(),
124 with libselinux < 2.1.5 2011-0826. */
125 newpath = canonicalize_filename_mode (path, CAN_MISSING);
126 if (! newpath)
127 die (EXIT_FAILURE, errno, _("error canonicalizing %s"),
128 quoteaf (path));
129 path = newpath;
132 if (matchpathcon (path, mode, &scon) < 0)
134 /* "No such file or directory" is a confusing error,
135 when processing files, when in fact it was the
136 associated default context that was not found.
137 Therefore map the error to something more appropriate
138 to the context in which we're using matchpathcon(). */
139 if (errno == ENOENT)
140 errno = ENODATA;
141 goto quit;
143 if (computecon (path, mode, &tcon) < 0)
144 goto quit;
145 if (!(scontext = context_new (scon)))
146 goto quit;
147 if (!(tcontext = context_new (tcon)))
148 goto quit;
150 if (!(contype = context_type_get (scontext)))
151 goto quit;
152 if (context_type_set (tcontext, contype))
153 goto quit;
154 if (!(constr = context_str (tcontext)))
155 goto quit;
157 rc = setfscreatecon (constr);
159 quit:
160 context_free (scontext);
161 context_free (tcontext);
162 freecon (scon);
163 freecon (tcon);
164 free (newpath);
165 return rc;
169 This function takes a PATH of an existing file system object, and a LOCAL
170 boolean that indicates whether the function should set the object's label
171 to the default for the local process, or one using system wide settings.
172 If LOCAL == true, it will ask the SELinux Kernel what the default label
173 for all objects created should be and then sets the label on the object.
174 Otherwise it calls matchpathcon on the object to ask the system what the
175 default label should be, extracts the type field and then modifies the file
176 system object. Note only the type field is updated, thus preserving MLS
177 levels and user identity etc. of the PATH.
179 Returns -1 on failure. errno will be set appropriately.
181 static int
182 restorecon_private (char const *path, bool local)
184 int rc = -1;
185 struct stat sb;
186 char *scon = NULL;
187 char *tcon = NULL;
188 context_t scontext = 0, tcontext = 0;
189 const char *contype;
190 char *constr;
191 int fd;
193 if (local)
195 if (getfscreatecon (&tcon) < 0)
196 return rc;
197 if (!tcon)
199 errno = ENODATA;
200 return rc;
202 rc = lsetfilecon (path, tcon);
203 freecon (tcon);
204 return rc;
207 fd = open (path, O_RDONLY | O_NOFOLLOW);
208 if (fd == -1 && (errno != ELOOP))
209 goto quit;
211 if (fd != -1)
213 if (fstat (fd, &sb) < 0)
214 goto quit;
216 else
218 if (lstat (path, &sb) < 0)
219 goto quit;
222 if (matchpathcon (path, sb.st_mode, &scon) < 0)
224 /* "No such file or directory" is a confusing error,
225 when processing files, when in fact it was the
226 associated default context that was not found.
227 Therefore map the error to something more appropriate
228 to the context in which we're using matchpathcon(). */
229 if (errno == ENOENT)
230 errno = ENODATA;
231 goto quit;
233 if (!(scontext = context_new (scon)))
234 goto quit;
236 if (fd != -1)
238 if (fgetfilecon (fd, &tcon) < 0)
239 goto quit;
241 else
243 if (lgetfilecon (path, &tcon) < 0)
244 goto quit;
247 if (!(tcontext = context_new (tcon)))
248 goto quit;
250 if (!(contype = context_type_get (scontext)))
251 goto quit;
252 if (context_type_set (tcontext, contype))
253 goto quit;
254 if (!(constr = context_str (tcontext)))
255 goto quit;
257 if (fd != -1)
258 rc = fsetfilecon (fd, constr);
259 else
260 rc = lsetfilecon (path, constr);
262 quit:
263 if (fd != -1)
264 close (fd);
265 context_free (scontext);
266 context_free (tcontext);
267 freecon (scon);
268 freecon (tcon);
269 return rc;
273 This function takes three parameters:
275 PATH of an existing file system object.
277 A RECURSE boolean which if the file system object is a directory, will
278 call restorecon_private on every file system object in the directory.
280 A LOCAL boolean that indicates whether the function should set object labels
281 to the default for the local process, or use system wide settings.
283 Returns false on failure. errno will be set appropriately.
285 bool
286 restorecon (char const *path, bool recurse, bool local)
288 char *newpath = NULL;
289 FTS *fts;
290 bool ok = true;
292 if (! IS_ABSOLUTE_FILE_NAME (path) && ! local)
294 /* Generate absolute path as required by subsequent matchpathcon(),
295 with libselinux < 2.1.5 2011-0826. Also generating the absolute
296 path before the fts walk, will generate absolute paths in the
297 fts entries, which may be quicker to process in any case. */
298 newpath = canonicalize_filename_mode (path, CAN_MISSING);
299 if (! newpath)
300 die (EXIT_FAILURE, errno, _("error canonicalizing %s"),
301 quoteaf (path));
304 const char *ftspath[2] = { newpath ? newpath : path, NULL };
306 if (! recurse)
308 ok = restorecon_private (*ftspath, local) != -1;
309 free (newpath);
310 return ok;
313 fts = xfts_open ((char *const *) ftspath, FTS_PHYSICAL, NULL);
314 while (1)
316 FTSENT *ent;
318 ent = fts_read (fts);
319 if (ent == NULL)
321 if (errno != 0)
323 error (0, errno, _("fts_read failed"));
324 ok = false;
326 break;
329 ok &= restorecon_private (fts->fts_path, local) != -1;
332 if (fts_close (fts) != 0)
334 error (0, errno, _("fts_close failed"));
335 ok = false;
338 free (newpath);
339 return ok;
341 #endif