1 /* selinux - core functions for maintaining SELinux labeling
2 Copyright (C) 2012-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Daniel Walsh <dwalsh@redhat.com> */
20 #include <selinux/label.h>
21 #include <selinux/context.h>
22 #include <sys/types.h>
25 #include "canonicalize.h"
29 #if HAVE_SELINUX_LABEL_H
31 # if ! HAVE_MODE_TO_SECURITY_CLASS
33 This function has been added to libselinux-2.1.12-5, but is here
34 for support with older versions of SELinux
36 Translates a mode into an Internal SELinux security_class definition.
37 Returns 0 on failure, with errno set to EINVAL.
39 static security_class_t
40 mode_to_security_class (mode_t m
)
44 return string_to_security_class ("file");
46 return string_to_security_class ("dir");
48 return string_to_security_class ("chr_file");
50 return string_to_security_class ("blk_file");
52 return string_to_security_class ("fifo_file");
54 return string_to_security_class ("lnk_file");
56 return string_to_security_class ("sock_file");
64 This function takes a PATH and a MODE and then asks SELinux what the label
65 of the path object would be if the current process label created it.
66 It then returns the label.
68 Returns -1 on failure. errno will be set appropriately.
72 computecon_raw (char const *path
, mode_t mode
, char **con
)
74 char *scon_raw
= nullptr;
75 char *tcon_raw
= nullptr;
76 security_class_t tclass
;
79 char *dir
= dir_name (path
);
82 if (getcon_raw (&scon_raw
) < 0)
84 if (getfilecon_raw (dir
, &tcon_raw
) < 0)
86 tclass
= mode_to_security_class (mode
);
89 rc
= security_compute_create_raw (scon_raw
, tcon_raw
, tclass
, con
);
101 This function takes a handle, path and mode, it calls computecon_raw to get
102 the label of the path object if the current process created it, then it calls
103 selabel_lookup 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 (struct selabel_handle
*selabel_handle
,
111 char const *path
, mode_t mode
)
114 char *scon_raw
= nullptr;
115 char *tcon_raw
= nullptr;
116 context_t scontext
= 0, tcontext
= 0;
119 char *newpath
= nullptr;
121 if (! IS_ABSOLUTE_FILE_NAME (path
))
123 /* Generate absolute name as required by subsequent selabel_lookup. */
124 newpath
= canonicalize_filename_mode (path
, CAN_MISSING
);
130 if (selabel_lookup_raw (selabel_handle
, &scon_raw
, path
, mode
) < 0)
132 /* "No such file or directory" is a confusing error,
133 when processing files, when in fact it was the
134 associated default context that was not found.
135 Therefore map the error to something more appropriate
136 to the context in which we're using selabel_lookup(). */
141 if (computecon_raw (path
, mode
, &tcon_raw
) < 0)
143 if (!(scontext
= context_new (scon_raw
)))
145 if (!(tcontext
= context_new (tcon_raw
)))
148 if (!(contype
= context_type_get (scontext
)))
150 if (context_type_set (tcontext
, contype
))
152 if (!(constr
= context_str (tcontext
)))
155 rc
= setfscreatecon_raw (constr
);
159 context_free (scontext
);
160 context_free (tcontext
);
169 If SELABEL_HANDLE is null, set PATH's label to the default to the
170 local process. Otherwise use selabel_lookup to determine the
171 default label, extract the type field and then modify the file
172 system object. Note only the type field is updated, thus preserving MLS
173 levels and user identity etc. of the PATH.
175 Returns -1 on failure. errno will be set appropriately.
178 restorecon_private (struct selabel_handle
*selabel_handle
, char const *path
)
182 char *scon_raw
= nullptr;
183 char *tcon_raw
= nullptr;
184 context_t scontext
= 0, tcontext
= 0;
191 if (getfscreatecon_raw (&tcon_raw
) < 0)
198 rc
= lsetfilecon_raw (path
, tcon_raw
);
205 fd
= open (path
, O_RDONLY
| O_NOFOLLOW
);
206 if (fd
== -1 && (errno
!= ELOOP
))
211 if (fstat (fd
, &sb
) < 0)
216 if (lstat (path
, &sb
) < 0)
220 if (selabel_lookup_raw (selabel_handle
, &scon_raw
, path
, sb
.st_mode
) < 0)
222 /* "No such file or directory" is a confusing error,
223 when processing files, when in fact it was the
224 associated default context that was not found.
225 Therefore map the error to something more appropriate
226 to the context in which we're using selabel_lookup. */
231 if (!(scontext
= context_new (scon_raw
)))
236 if (fgetfilecon_raw (fd
, &tcon_raw
) < 0)
241 if (lgetfilecon_raw (path
, &tcon_raw
) < 0)
245 if (!(tcontext
= context_new (tcon_raw
)))
248 if (!(contype
= context_type_get (scontext
)))
250 if (context_type_set (tcontext
, contype
))
252 if (!(constr
= context_str (tcontext
)))
256 rc
= fsetfilecon_raw (fd
, constr
);
258 rc
= lsetfilecon_raw (path
, constr
);
264 context_free (scontext
);
265 context_free (tcontext
);
273 This function takes three parameters:
275 SELABEL_HANDLE for selabel_lookup, or null to preserve.
277 PATH of an existing file system object.
279 A RECURSE boolean which if the file system object is a directory, will
280 call restorecon_private on every file system object in the directory.
282 Return false on failure. errno will be set appropriately.
285 restorecon (struct selabel_handle
*selabel_handle
,
286 char const *path
, bool recurse
)
288 char *newpath
= nullptr;
290 if (! IS_ABSOLUTE_FILE_NAME (path
))
292 /* Generate absolute name as required by subsequent selabel_lookup.
293 When RECURSE, this also generates absolute names in the
294 fts entries, which may be quicker to process in any case. */
295 newpath
= canonicalize_filename_mode (path
, CAN_MISSING
);
303 bool ok
= restorecon_private (selabel_handle
, path
) != -1;
310 char const *ftspath
[2] = { path
, nullptr };
311 FTS
*fts
= xfts_open ((char *const *) ftspath
, FTS_PHYSICAL
, nullptr);
314 for (FTSENT
*ent
; (ent
= fts_read (fts
)); )
315 if (restorecon_private (selabel_handle
, fts
->fts_path
) < 0)
321 if (fts_close (fts
) != 0)