1 /* selinux - core functions for maintaining SELinux labeling
2 Copyright (C) 2012-2013 Red Hat, 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
, security_context_t
* con
)
77 security_context_t scon
= NULL
;
78 security_context_t tcon
= NULL
;
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
)
114 security_context_t scon
= NULL
, tcon
= NULL
;
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
)
185 security_context_t scon
= NULL
, tcon
= NULL
;
186 context_t scontext
= 0, tcontext
= 0;
193 if (getfscreatecon (&tcon
) < 0)
195 rc
= lsetfilecon (path
, tcon
);
200 fd
= open (path
, O_RDONLY
| O_NOFOLLOW
);
201 if (fd
== -1 && (errno
!= ELOOP
))
206 if (fstat (fd
, &sb
) < 0)
211 if (lstat (path
, &sb
) < 0)
215 if (matchpathcon (path
, sb
.st_mode
, &scon
) < 0)
217 /* "No such file or directory" is a confusing error,
218 when processing files, when in fact it was the
219 associated default context that was not found.
220 Therefore map the error to something more appropriate
221 to the context in which we're using matchpathcon(). */
226 if (!(scontext
= context_new (scon
)))
231 if (fgetfilecon (fd
, &tcon
) < 0)
236 if (lgetfilecon (path
, &tcon
) < 0)
240 if (!(tcontext
= context_new (tcon
)))
243 if (!(contype
= context_type_get (scontext
)))
245 if (context_type_set (tcontext
, contype
))
247 if (!(constr
= context_str (tcontext
)))
251 rc
= fsetfilecon (fd
, constr
);
253 rc
= lsetfilecon (path
, constr
);
258 context_free (scontext
);
259 context_free (tcontext
);
266 This function takes three parameters:
268 PATH of an existing file system object.
270 A RECURSE boolean which if the file system object is a directory, will
271 call restorecon_private on every file system object in the directory.
273 A LOCAL boolean that indicates whether the function should set object labels
274 to the default for the local process, or use system wide settings.
276 Returns false on failure. errno will be set appropriately.
279 restorecon (char const *path
, bool recurse
, bool local
)
281 char *newpath
= NULL
;
285 if (! IS_ABSOLUTE_FILE_NAME (path
) && ! local
)
287 /* Generate absolute path as required by subsequent matchpathcon(),
288 with libselinux < 2.1.5 2011-0826. Also generating the absolute
289 path before the fts walk, will generate absolute paths in the
290 fts entries, which may be quicker to process in any case. */
291 newpath
= canonicalize_filename_mode (path
, CAN_MISSING
);
293 error (EXIT_FAILURE
, errno
, _("error canonicalizing %s"),
297 const char *ftspath
[2] = { newpath
? newpath
: path
, NULL
};
301 ok
= restorecon_private (*ftspath
, local
) != -1;
306 fts
= xfts_open ((char *const *) ftspath
, FTS_PHYSICAL
, NULL
);
311 ent
= fts_read (fts
);
316 error (0, errno
, _("fts_read failed"));
322 ok
&= restorecon_private (fts
->fts_path
, local
) != -1;
325 if (fts_close (fts
) != 0)
327 error (0, errno
, _("fts_close failed"));