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> */
20 #include <selinux/selinux.h>
21 #include <selinux/context.h>
22 #include <sys/types.h>
26 #include "canonicalize.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
)
47 return string_to_security_class ("file");
49 return string_to_security_class ("dir");
51 return string_to_security_class ("chr_file");
53 return string_to_security_class ("blk_file");
55 return string_to_security_class ("fifo_file");
57 return string_to_security_class ("lnk_file");
59 return string_to_security_class ("sock_file");
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.
75 computecon (char const *path
, mode_t mode
, char **con
)
79 security_class_t tclass
;
82 char *dir
= dir_name (path
);
85 if (getcon (&scon
) < 0)
87 if (getfilecon (dir
, &tcon
) < 0)
89 tclass
= mode_to_security_class (mode
);
92 rc
= security_compute_create (scon
, tcon
, tclass
, con
);
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
)
116 context_t scontext
= 0, tcontext
= 0;
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
);
127 error (EXIT_FAILURE
, errno
, _("error canonicalizing %s"),
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(). */
143 if (computecon (path
, mode
, &tcon
) < 0)
145 if (!(scontext
= context_new (scon
)))
147 if (!(tcontext
= context_new (tcon
)))
150 if (!(contype
= context_type_get (scontext
)))
152 if (context_type_set (tcontext
, contype
))
154 if (!(constr
= context_str (tcontext
)))
157 rc
= setfscreatecon (constr
);
160 context_free (scontext
);
161 context_free (tcontext
);
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.
182 restorecon_private (char const *path
, bool local
)
188 context_t scontext
= 0, tcontext
= 0;
195 if (getfscreatecon (&tcon
) < 0)
202 rc
= lsetfilecon (path
, tcon
);
207 fd
= open (path
, O_RDONLY
| O_NOFOLLOW
);
208 if (fd
== -1 && (errno
!= ELOOP
))
213 if (fstat (fd
, &sb
) < 0)
218 if (lstat (path
, &sb
) < 0)
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(). */
233 if (!(scontext
= context_new (scon
)))
238 if (fgetfilecon (fd
, &tcon
) < 0)
243 if (lgetfilecon (path
, &tcon
) < 0)
247 if (!(tcontext
= context_new (tcon
)))
250 if (!(contype
= context_type_get (scontext
)))
252 if (context_type_set (tcontext
, contype
))
254 if (!(constr
= context_str (tcontext
)))
258 rc
= fsetfilecon (fd
, constr
);
260 rc
= lsetfilecon (path
, constr
);
265 context_free (scontext
);
266 context_free (tcontext
);
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.
286 restorecon (char const *path
, bool recurse
, bool local
)
288 char *newpath
= NULL
;
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
);
300 error (EXIT_FAILURE
, errno
, _("error canonicalizing %s"),
304 const char *ftspath
[2] = { newpath
? newpath
: path
, NULL
};
308 ok
= restorecon_private (*ftspath
, local
) != -1;
313 fts
= xfts_open ((char *const *) ftspath
, FTS_PHYSICAL
, NULL
);
318 ent
= fts_read (fts
);
323 error (0, errno
, _("fts_read failed"));
329 ok
&= restorecon_private (fts
->fts_path
, local
) != -1;
332 if (fts_close (fts
) != 0)
334 error (0, errno
, _("fts_close failed"));