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"
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
)
46 return string_to_security_class ("file");
48 return string_to_security_class ("dir");
50 return string_to_security_class ("chr_file");
52 return string_to_security_class ("blk_file");
54 return string_to_security_class ("fifo_file");
56 return string_to_security_class ("lnk_file");
58 return string_to_security_class ("sock_file");
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.
74 computecon (char const *path
, mode_t mode
, char **con
)
78 security_class_t tclass
;
81 char *dir
= dir_name (path
);
84 if (getcon (&scon
) < 0)
86 if (getfilecon (dir
, &tcon
) < 0)
88 tclass
= mode_to_security_class (mode
);
91 rc
= security_compute_create (scon
, tcon
, tclass
, con
);
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
)
115 context_t scontext
= 0, tcontext
= 0;
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
);
126 error (EXIT_FAILURE
, errno
, _("error canonicalizing %s"),
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(). */
142 if (computecon (path
, mode
, &tcon
) < 0)
144 if (!(scontext
= context_new (scon
)))
146 if (!(tcontext
= context_new (tcon
)))
149 if (!(contype
= context_type_get (scontext
)))
151 if (context_type_set (tcontext
, contype
))
153 if (!(constr
= context_str (tcontext
)))
156 rc
= setfscreatecon (constr
);
159 context_free (scontext
);
160 context_free (tcontext
);
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.
181 restorecon_private (char const *path
, bool local
)
187 context_t scontext
= 0, tcontext
= 0;
194 if (getfscreatecon (&tcon
) < 0)
201 rc
= lsetfilecon (path
, tcon
);
206 fd
= open (path
, O_RDONLY
| O_NOFOLLOW
);
207 if (fd
== -1 && (errno
!= ELOOP
))
212 if (fstat (fd
, &sb
) < 0)
217 if (lstat (path
, &sb
) < 0)
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(). */
232 if (!(scontext
= context_new (scon
)))
237 if (fgetfilecon (fd
, &tcon
) < 0)
242 if (lgetfilecon (path
, &tcon
) < 0)
246 if (!(tcontext
= context_new (tcon
)))
249 if (!(contype
= context_type_get (scontext
)))
251 if (context_type_set (tcontext
, contype
))
253 if (!(constr
= context_str (tcontext
)))
257 rc
= fsetfilecon (fd
, constr
);
259 rc
= lsetfilecon (path
, constr
);
264 context_free (scontext
);
265 context_free (tcontext
);
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.
285 restorecon (char const *path
, bool recurse
, bool local
)
287 char *newpath
= NULL
;
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
);
299 error (EXIT_FAILURE
, errno
, _("error canonicalizing %s"),
303 const char *ftspath
[2] = { newpath
? newpath
: path
, NULL
};
307 ok
= restorecon_private (*ftspath
, local
) != -1;
312 fts
= xfts_open ((char *const *) ftspath
, FTS_PHYSICAL
, NULL
);
317 ent
= fts_read (fts
);
322 error (0, errno
, _("fts_read failed"));
328 ok
&= restorecon_private (fts
->fts_path
, local
) != -1;
331 if (fts_close (fts
) != 0)
333 error (0, errno
, _("fts_close failed"));