cp: set SELinux context for --parents directories

* src/copy.c (set_process_security_ctx, set_file_security_ctx):
Export for use in cp.c.
* src/copy.h: Likewise.
* src/cp.c (make_dir_parents_private): Call the exported functions
to set the security context for new and updated directories.
* tests/cp/cp-a-selinux.sh: Add a test case.

Fixes http://bugs.gnu.org/25378
This commit is contained in:
Pádraig Brady committed 2017-02-22 20:23:31 -08:00
1 parent e4a4427502
commit db277d6077
5 files changed
+55 -4

No files matched your search

+2 -2
View File
@@ -889,7 +889,7 @@ set_author (const char *dst_name, int dest_desc, const struct stat *src_sb)
Based on CP_OPTIONS, diagnose warnings and fail when appropriate.
Return FALSE on failure, TRUE on success. */
static bool
bool
set_process_security_ctx (char const *src_name, char const *dst_name,
mode_t mode, bool new_dst, const struct cp_options *x)
{
@@ -951,7 +951,7 @@ set_process_security_ctx (char const *src_name, char const *dst_name,
failure, when allowed by various settings in CP_OPTIONS.
Return FALSE on failure, TRUE on success. */
static bool
bool
set_file_security_ctx (char const *dst_name, bool process_local,
bool recurse, const struct cp_options *x)
{
+8
View File
@@ -285,6 +285,14 @@ bool copy (char const *src_name, char const *dst_name,
bool nonexistent_dst, const struct cp_options *options,
bool *copy_into_self, bool *rename_succeeded);
extern bool set_process_security_ctx (char const *src_name,
char const *dst_name,
mode_t mode, bool new_dst,
const struct cp_options *x);
extern bool set_file_security_ctx (char const *dst_name, bool process_local,
bool recurse, const struct cp_options *x);
void dest_info_init (struct cp_options *);
void src_info_init (struct cp_options *);
+20
View File
@@ -394,6 +394,8 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
*attr_list = NULL;
/* XXX: If all dirs are present at the destination,
no permissions or security contexts will be updated. */
if (stat (dst_dir, &stats) != 0)
{
/* A parent of CONST_DIR does not exist.
@@ -437,6 +439,12 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
*attr_list = new;
}
/* If required set the default context for created dirs. */
if (! set_process_security_ctx (src, dir,
missing_dir ? new->st.st_mode : 0,
missing_dir, x))
return false;
if (missing_dir)
{
mode_t src_mode;
@@ -524,6 +532,18 @@ make_dir_parents_private (char const *const_dir, size_t src_offset,
}
else
*new_dst = false;
/* For existing dirs, set the security context as per that already
set for the process global context. */
if (! *new_dst
&& (x->set_security_context || x->preserve_security_context))
{
if (! set_file_security_ctx (dir, x->preserve_security_context,
false, x)
&& x->require_preserve_context)
return false;
}
*slash++ = '/';
/* Avoid unnecessary calls to 'stat' when given