2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 2001, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 - 2010, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2009-04-28 01:39:18 +05:30
|
|
|
#include <assert.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2007-12-26 19:24:23 +05:30
|
|
|
#include <sys/time.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
|
|
|
#include <selinux/selinux.h>
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2010-03-31 02:31:27 +05:30
|
|
|
#if defined(WITH_ACL) || defined(WITH_ATTR)
|
2012-02-14 00:46:29 +05:30
|
|
|
#include <stdarg.h>
|
2010-03-31 02:31:27 +05:30
|
|
|
#include <attr/error_context.h>
|
|
|
|
#endif /* WITH_ACL || WITH_ATTR */
|
|
|
|
#ifdef WITH_ACL
|
|
|
|
#include <acl/libacl.h>
|
|
|
|
#endif /* WITH_ACL */
|
|
|
|
#ifdef WITH_ATTR
|
|
|
|
#include <attr/libattr.h>
|
|
|
|
#endif /* WITH_ATTR */
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2010-03-31 02:31:27 +05:30
|
|
|
|
2010-08-29 01:28:00 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@null@*/const char *src_orig;
|
|
|
|
static /*@null@*/const char *dst_orig;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
struct link_name {
|
|
|
|
dev_t ln_dev;
|
|
|
|
ino_t ln_ino;
|
2009-04-25 05:11:28 +05:30
|
|
|
nlink_t ln_count;
|
2007-10-07 17:15:23 +05:30
|
|
|
char *ln_name;
|
2009-04-25 05:11:28 +05:30
|
|
|
/*@dependent@*/struct link_name *ln_next;
|
2007-10-07 17:14:02 +05:30
|
|
|
};
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@exposed@*/struct link_name *links;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
struct path_info {
|
|
|
|
const char *full_path;
|
|
|
|
int dirfd;
|
|
|
|
const char *name;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int copy_entry (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_dir (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2010-08-22 18:19:07 +05:30
|
|
|
static /*@null@*/char *readlink_malloc (const char *filename);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_symlink (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
unused bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_hardlink (const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
unused bool reset_selinux,
|
2007-12-28 04:53:51 +05:30
|
|
|
struct link_name *lp);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_special (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_file (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2022-08-05 21:27:32 +05:30
|
|
|
static int chownat_if_needed (const struct path_info *dst, const struct stat *statp,
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
|
|
|
static int fchown_if_needed (int fdst, const struct stat *statp,
|
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2010-03-31 02:31:27 +05:30
|
|
|
#if defined(WITH_ACL) || defined(WITH_ATTR)
|
2010-04-05 02:25:46 +05:30
|
|
|
/*
|
2011-11-11 17:29:21 +05:30
|
|
|
* error_acl - format the error messages for the ACL and EQ libraries.
|
2010-04-05 02:25:46 +05:30
|
|
|
*/
|
2022-08-05 21:10:42 +05:30
|
|
|
format_attr(printf, 2, 3)
|
|
|
|
static void error_acl (unused struct error_context *ctx, const char *fmt, ...)
|
2010-03-31 02:31:27 +05:30
|
|
|
{
|
|
|
|
va_list ap;
|
2021-11-29 05:07:53 +05:30
|
|
|
FILE *shadow_logfd = log_get_logfd();
|
2010-03-31 02:31:27 +05:30
|
|
|
|
2021-08-18 23:36:02 +05:30
|
|
|
/* ignore the case when destination does not support ACLs
|
2011-11-17 02:30:43 +05:30
|
|
|
* or extended attributes */
|
|
|
|
if (ENOTSUP == errno) {
|
2011-11-22 01:04:22 +05:30
|
|
|
errno = 0;
|
2011-11-17 02:30:43 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-31 02:31:27 +05:30
|
|
|
va_start (ap, fmt);
|
2021-11-29 05:07:53 +05:30
|
|
|
(void) fprintf (shadow_logfd, _("%s: "), log_get_progname());
|
2021-05-09 04:12:14 +05:30
|
|
|
if (vfprintf (shadow_logfd, fmt, ap) != 0) {
|
|
|
|
(void) fputs (_(": "), shadow_logfd);
|
2010-03-31 02:31:27 +05:30
|
|
|
}
|
2021-05-09 04:12:14 +05:30
|
|
|
(void) fprintf (shadow_logfd, "%s\n", strerror (errno));
|
2010-03-31 02:31:27 +05:30
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2010-04-05 02:25:46 +05:30
|
|
|
static struct error_context ctx = {
|
2022-08-05 21:27:29 +05:30
|
|
|
error_acl, NULL, NULL
|
2010-03-31 02:31:27 +05:30
|
|
|
};
|
|
|
|
#endif /* WITH_ACL || WITH_ATTR */
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
#ifdef WITH_ACL
|
|
|
|
static int perm_copy_path(const struct path_info *src,
|
|
|
|
const struct path_info *dst,
|
|
|
|
struct error_context *errctx)
|
|
|
|
{
|
|
|
|
int src_fd, dst_fd, ret;
|
|
|
|
|
2022-09-04 17:28:03 +05:30
|
|
|
src_fd = openat(src->dirfd, src->name, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
|
2022-08-05 21:27:32 +05:30
|
|
|
if (src_fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-09-04 17:28:03 +05:30
|
|
|
dst_fd = openat(dst->dirfd, dst->name, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
|
2022-08-05 21:27:32 +05:30
|
|
|
if (dst_fd < 0) {
|
|
|
|
(void) close (src_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = perm_copy_fd(src->full_path, src_fd, dst->full_path, dst_fd, errctx);
|
|
|
|
(void) close (src_fd);
|
|
|
|
(void) close (dst_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* WITH_ACL */
|
|
|
|
|
|
|
|
#ifdef WITH_ATTR
|
|
|
|
static int attr_copy_path(const struct path_info *src,
|
|
|
|
const struct path_info *dst,
|
|
|
|
int (*callback) (const char *, struct error_context *),
|
|
|
|
struct error_context *errctx)
|
|
|
|
{
|
|
|
|
int src_fd, dst_fd, ret;
|
|
|
|
|
2022-09-04 17:28:03 +05:30
|
|
|
src_fd = openat(src->dirfd, src->name, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
|
2022-08-05 21:27:32 +05:30
|
|
|
if (src_fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-09-04 17:28:03 +05:30
|
|
|
dst_fd = openat(dst->dirfd, dst->name, O_RDONLY | O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC);
|
2022-08-05 21:27:32 +05:30
|
|
|
if (dst_fd < 0) {
|
|
|
|
(void) close (src_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = attr_copy_fd(src->full_path, src_fd, dst->full_path, dst_fd, callback, errctx);
|
|
|
|
(void) close (src_fd);
|
|
|
|
(void) close (dst_fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* WITH_ATTR */
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2007-12-28 05:53:33 +05:30
|
|
|
* remove_link - delete a link from the linked list
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
2009-04-25 05:11:28 +05:30
|
|
|
static void remove_link (/*@only@*/struct link_name *ln)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct link_name *lp;
|
|
|
|
|
|
|
|
if (links == ln) {
|
|
|
|
links = ln->ln_next;
|
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
|
|
|
return;
|
|
|
|
}
|
2007-12-28 05:33:26 +05:30
|
|
|
for (lp = links; NULL !=lp; lp = lp->ln_next) {
|
2007-12-28 05:10:00 +05:30
|
|
|
if (lp->ln_next == ln) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 05:33:26 +05:30
|
|
|
if (NULL == lp) {
|
2009-04-25 05:11:28 +05:30
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
2007-10-07 17:14:02 +05:30
|
|
|
return;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
lp->ln_next = lp->ln_next->ln_next;
|
|
|
|
free (ln->ln_name);
|
|
|
|
free (ln);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check_link - see if a file is really a link
|
|
|
|
*/
|
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, const struct stat *sb)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
struct link_name *lp;
|
2008-06-14 01:40:53 +05:30
|
|
|
size_t src_len;
|
|
|
|
size_t dst_len;
|
|
|
|
size_t name_len;
|
|
|
|
size_t len;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* copy_tree () must be the entry point */
|
|
|
|
assert (NULL != src_orig);
|
|
|
|
assert (NULL != dst_orig);
|
|
|
|
|
|
|
|
for (lp = links; NULL != lp; lp = lp->ln_next) {
|
2007-12-28 05:33:26 +05:30
|
|
|
if ((lp->ln_dev == sb->st_dev) && (lp->ln_ino == sb->st_ino)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return lp;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-12-28 05:10:00 +05:30
|
|
|
if (sb->st_nlink == 1) {
|
2007-12-28 05:11:36 +05:30
|
|
|
return NULL;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
lp = (struct link_name *) xmalloc (sizeof *lp);
|
|
|
|
src_len = strlen (src_orig);
|
|
|
|
dst_len = strlen (dst_orig);
|
|
|
|
name_len = strlen (name);
|
|
|
|
lp->ln_dev = sb->st_dev;
|
|
|
|
lp->ln_ino = sb->st_ino;
|
|
|
|
lp->ln_count = sb->st_nlink;
|
|
|
|
len = name_len - src_len + dst_len + 1;
|
2008-06-14 01:40:53 +05:30
|
|
|
lp->ln_name = (char *) xmalloc (len);
|
2010-08-22 18:19:07 +05:30
|
|
|
(void) snprintf (lp->ln_name, len, "%s%s", dst_orig, name + src_len);
|
2007-10-07 17:14:02 +05:30
|
|
|
lp->ln_next = links;
|
|
|
|
links = lp;
|
|
|
|
|
2007-12-28 05:11:36 +05:30
|
|
|
return NULL;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_tree_impl (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool copy_root, bool reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2022-08-05 21:27:32 +05:30
|
|
|
int dst_fd, src_fd, err = 0;
|
2008-05-26 05:15:21 +05:30
|
|
|
bool set_orig = false;
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct dirent *ent;
|
2007-10-07 17:15:23 +05:30
|
|
|
DIR *dir;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2010-04-05 02:25:46 +05:30
|
|
|
if (copy_root) {
|
|
|
|
struct stat sb;
|
2022-08-05 21:27:32 +05:30
|
|
|
|
|
|
|
if ( fstatat (dst->dirfd, dst->name, &sb, 0) == 0
|
|
|
|
|| errno != ENOENT) {
|
2010-04-05 02:25:46 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if (fstatat (src->dirfd, src->name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
|
2010-04-05 02:25:46 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!S_ISDIR (sb.st_mode)) {
|
2021-11-29 05:07:53 +05:30
|
|
|
fprintf (log_get_logfd(),
|
2010-04-05 02:25:46 +05:30
|
|
|
"%s: %s is not a directory",
|
2022-08-05 21:27:32 +05:30
|
|
|
log_get_progname(), src->full_path);
|
2010-04-05 02:25:46 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
return copy_entry (src, dst, reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Make certain both directories exist. This routine is called
|
|
|
|
* after the home directory is created, or recursively after the
|
|
|
|
* target is created. It assumes the target directory exists.
|
|
|
|
*/
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
src_fd = openat (src->dirfd, src->name, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
|
|
|
if (src_fd < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst_fd = openat (dst->dirfd, dst->name, O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
|
|
|
|
if (dst_fd < 0) {
|
|
|
|
(void) close (src_fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the source directory and read each entry. Every file
|
|
|
|
* entry in the directory is copied with the UID and GID set
|
|
|
|
* to the provided values. As an added security feature only
|
|
|
|
* regular files (and directories ...) are copied, and no file
|
|
|
|
* is made set-ID.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
dir = fdopendir (src_fd);
|
2007-12-28 05:10:00 +05:30
|
|
|
if (NULL == dir) {
|
2022-08-05 21:27:32 +05:30
|
|
|
(void) close (src_fd);
|
|
|
|
(void) close (dst_fd);
|
2007-10-07 17:14:02 +05:30
|
|
|
return -1;
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-26 05:15:21 +05:30
|
|
|
if (src_orig == NULL) {
|
2022-08-05 21:27:32 +05:30
|
|
|
src_orig = src->full_path;
|
|
|
|
dst_orig = dst->full_path;
|
2008-05-26 05:15:21 +05:30
|
|
|
set_orig = true;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 05:38:16 +05:30
|
|
|
while ((0 == err) && (ent = readdir (dir)) != NULL) {
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Skip the "." and ".." entries
|
|
|
|
*/
|
2007-12-28 05:53:33 +05:30
|
|
|
if ((strcmp (ent->d_name, ".") != 0) &&
|
|
|
|
(strcmp (ent->d_name, "..") != 0)) {
|
2009-05-10 19:19:03 +05:30
|
|
|
char *src_name;
|
|
|
|
char *dst_name;
|
|
|
|
size_t src_len = strlen (ent->d_name) + 2;
|
|
|
|
size_t dst_len = strlen (ent->d_name) + 2;
|
2022-08-05 21:27:32 +05:30
|
|
|
src_len += strlen (src->full_path);
|
|
|
|
dst_len += strlen (dst->full_path);
|
2009-05-10 19:19:03 +05:30
|
|
|
|
|
|
|
src_name = (char *) malloc (src_len);
|
|
|
|
dst_name = (char *) malloc (dst_len);
|
|
|
|
|
|
|
|
if ((NULL == src_name) || (NULL == dst_name)) {
|
2007-12-28 05:53:33 +05:30
|
|
|
err = -1;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Build the filename for both the source and
|
|
|
|
* the destination files.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
struct path_info src_entry, dst_entry;
|
|
|
|
|
2010-08-22 18:19:07 +05:30
|
|
|
(void) snprintf (src_name, src_len, "%s/%s",
|
2022-08-05 21:27:32 +05:30
|
|
|
src->full_path, ent->d_name);
|
2010-08-22 18:19:07 +05:30
|
|
|
(void) snprintf (dst_name, dst_len, "%s/%s",
|
2022-08-05 21:27:32 +05:30
|
|
|
dst->full_path, ent->d_name);
|
|
|
|
|
|
|
|
src_entry.full_path = src_name;
|
|
|
|
src_entry.dirfd = dirfd(dir);
|
|
|
|
src_entry.name = ent->d_name;
|
2007-12-28 05:53:33 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
dst_entry.full_path = dst_name;
|
|
|
|
dst_entry.dirfd = dst_fd;
|
|
|
|
dst_entry.name = ent->d_name;
|
|
|
|
|
|
|
|
err = copy_entry (&src_entry, &dst_entry,
|
2010-08-21 23:01:45 +05:30
|
|
|
reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid,
|
|
|
|
old_gid, new_gid);
|
2007-12-28 05:53:33 +05:30
|
|
|
}
|
2022-09-29 01:33:52 +05:30
|
|
|
free (src_name);
|
|
|
|
free (dst_name);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) closedir (dir);
|
2022-08-05 21:27:32 +05:30
|
|
|
(void) close (dst_fd);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
|
|
|
if (set_orig) {
|
2008-05-26 05:15:21 +05:30
|
|
|
src_orig = NULL;
|
|
|
|
dst_orig = NULL;
|
2010-03-31 04:44:04 +05:30
|
|
|
/* FIXME: clean links
|
|
|
|
* Since there can be hardlinks elsewhere on the device,
|
|
|
|
* we cannot check that all the hardlinks were found:
|
|
|
|
assert (NULL == links);
|
|
|
|
*/
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2009-04-11 21:04:10 +05:30
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
2010-08-29 01:28:00 +05:30
|
|
|
/* Reset SELinux to create files with default contexts.
|
|
|
|
* Note that the context is only reset on exit of copy_tree (it is
|
|
|
|
* assumed that the program would quit without needing a restored
|
|
|
|
* context if copy_tree failed previously), and that copy_tree can
|
|
|
|
* be called recursively (hence the context is set on the
|
|
|
|
* sub-functions of copy_entry).
|
|
|
|
*/
|
|
|
|
if (reset_selinux_file_context () != 0) {
|
2010-08-22 18:19:07 +05:30
|
|
|
err = -1;
|
|
|
|
}
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2009-04-11 21:04:10 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_entry - copy the entry of a directory
|
|
|
|
*
|
|
|
|
* Copy the entry src to dst.
|
|
|
|
* Depending on the type of entry, this function will forward the
|
|
|
|
* request to copy_dir(), copy_symlink(), copy_hardlink(),
|
|
|
|
* copy_special(), or copy_file().
|
|
|
|
*
|
|
|
|
* The access and modification time will not be modified.
|
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* The permissions will be set to new_uid/new_gid.
|
2007-12-28 06:05:41 +05:30
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* If new_uid (resp. new_gid) is equal to -1, the user (resp. group) will
|
2007-12-28 06:05:41 +05:30
|
|
|
* not be modified.
|
2010-04-05 02:25:46 +05:30
|
|
|
*
|
|
|
|
* Only the files owned (resp. group-owned) by old_uid (resp.
|
|
|
|
* old_gid) will be modified, unless old_uid (resp. old_gid) is set
|
|
|
|
* to -1.
|
2007-12-28 06:05:41 +05:30
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_entry (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
struct stat sb;
|
|
|
|
struct link_name *lp;
|
2022-08-05 21:27:32 +05:30
|
|
|
struct timespec mt[2];
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if (fstatat(src->dirfd, src->name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
|
2007-12-28 04:53:51 +05:30
|
|
|
/* If we cannot stat the file, do not care. */
|
|
|
|
} else {
|
2007-12-26 19:24:23 +05:30
|
|
|
mt[0].tv_sec = sb.st_atim.tv_sec;
|
2022-08-05 21:27:32 +05:30
|
|
|
mt[0].tv_nsec = sb.st_atim.tv_nsec;
|
2022-12-22 16:12:44 +05:30
|
|
|
|
2008-09-07 05:35:38 +05:30
|
|
|
mt[1].tv_sec = sb.st_mtim.tv_sec;
|
2022-08-05 21:27:32 +05:30
|
|
|
mt[1].tv_nsec = sb.st_mtim.tv_nsec;
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (S_ISDIR (sb.st_mode)) {
|
2010-08-21 23:01:45 +05:30
|
|
|
err = copy_dir (src, dst, reset_selinux, &sb, mt,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2022-11-09 19:11:31 +05:30
|
|
|
/*
|
|
|
|
* If the destination already exists do nothing.
|
|
|
|
* This is after the copy_dir above to still iterate into subdirectories.
|
|
|
|
*/
|
|
|
|
if (fstatat(dst->dirfd, dst->name, &sb, AT_SYMLINK_NOFOLLOW) != -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* Copy any symbolic links
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else if (S_ISLNK (sb.st_mode)) {
|
2010-08-21 23:01:45 +05:30
|
|
|
err = copy_symlink (src, dst, reset_selinux, &sb, mt,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if this is a previously copied link
|
|
|
|
*/
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
else if ((lp = check_link (src->full_path, &sb)) != NULL) {
|
2010-08-21 23:01:45 +05:30
|
|
|
err = copy_hardlink (dst, reset_selinux, lp);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deal with FIFOs and special files. The user really
|
|
|
|
* shouldn't have any of these, but it seems like it
|
|
|
|
* would be nice to copy everything ...
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else if (!S_ISREG (sb.st_mode)) {
|
2010-08-21 23:01:45 +05:30
|
|
|
err = copy_special (src, dst, reset_selinux, &sb, mt,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the new file and copy the contents. The new
|
|
|
|
* file will be owned by the provided UID and GID values.
|
|
|
|
*/
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
else {
|
2010-08-21 23:01:45 +05:30
|
|
|
err = copy_file (src, dst, reset_selinux, &sb, mt,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_dir - copy a directory
|
|
|
|
*
|
|
|
|
* Copy a directory (recursively) from src to dst.
|
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* statp, mt, old_uid, new_uid, old_gid, and new_gid are used to set
|
|
|
|
* the access and modification and the access rights.
|
2007-12-28 06:05:41 +05:30
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_dir (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
2022-11-09 19:11:31 +05:30
|
|
|
struct stat dst_sb;
|
2007-12-28 04:53:51 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new target directory, make it owned by
|
|
|
|
* the user and then recursively copy that directory.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifdef WITH_SELINUX
|
2022-08-05 21:27:32 +05:30
|
|
|
if (set_selinux_file_context (dst->full_path, S_IFDIR) != 0) {
|
2010-08-22 18:19:07 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2022-11-09 19:11:31 +05:30
|
|
|
/*
|
|
|
|
* If the destination is already a directory, don't change it
|
|
|
|
* but copy into it (recursively).
|
|
|
|
*/
|
|
|
|
if (fstatat(dst->dirfd, dst->name, &dst_sb, AT_SYMLINK_NOFOLLOW) == 0 && S_ISDIR(dst_sb.st_mode)) {
|
|
|
|
return (copy_tree (src, dst, false, reset_selinux,
|
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0);
|
|
|
|
}
|
|
|
|
|
2022-09-10 17:28:15 +05:30
|
|
|
if ( (mkdirat (dst->dirfd, dst->name, 0700) != 0)
|
2022-08-05 21:27:32 +05:30
|
|
|
|| (chownat_if_needed (dst, statp,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0)
|
2022-09-10 17:28:15 +05:30
|
|
|
|| (fchmodat (dst->dirfd, dst->name, statp->st_mode & 07777, AT_SYMLINK_NOFOLLOW) != 0)
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ACL
|
2022-08-05 21:27:32 +05:30
|
|
|
|| ( (perm_copy_path (src, dst, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2022-09-10 17:28:15 +05:30
|
|
|
#endif /* WITH_ACL */
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ATTR
|
|
|
|
/*
|
|
|
|
* If the third parameter is NULL, all extended attributes
|
|
|
|
* except those that define Access Control Lists are copied.
|
|
|
|
* ACLs are excluded by default because copying them between
|
|
|
|
* file systems with and without ACL support needs some
|
|
|
|
* additional logic so that no unexpected permissions result.
|
|
|
|
*/
|
2011-11-22 01:04:22 +05:30
|
|
|
|| ( !reset_selinux
|
2022-08-05 21:27:32 +05:30
|
|
|
&& (attr_copy_path (src, dst, NULL, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2010-03-31 02:31:27 +05:30
|
|
|
#endif /* WITH_ATTR */
|
2022-08-05 21:27:32 +05:30
|
|
|
|| (copy_tree_impl (src, dst, false, reset_selinux,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0)
|
2022-08-05 21:27:32 +05:30
|
|
|
|| (utimensat (dst->dirfd, dst->name, mt, AT_SYMLINK_NOFOLLOW) != 0)) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-05-10 19:19:03 +05:30
|
|
|
/*
|
|
|
|
* readlink_malloc - wrapper for readlink
|
|
|
|
*
|
|
|
|
* return NULL on error.
|
|
|
|
* The return string shall be freed by the caller.
|
|
|
|
*/
|
2010-08-22 18:19:07 +05:30
|
|
|
static /*@null@*/char *readlink_malloc (const char *filename)
|
2009-05-10 19:19:03 +05:30
|
|
|
{
|
|
|
|
size_t size = 1024;
|
|
|
|
|
2010-08-22 18:19:07 +05:30
|
|
|
while (true) {
|
2009-05-10 19:19:03 +05:30
|
|
|
ssize_t nchars;
|
|
|
|
char *buffer = (char *) malloc (size);
|
|
|
|
if (NULL == buffer) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
nchars = readlink (filename, buffer, size);
|
|
|
|
|
|
|
|
if (nchars < 0) {
|
2009-09-08 00:23:47 +05:30
|
|
|
free(buffer);
|
2009-05-10 19:19:03 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-22 18:19:07 +05:30
|
|
|
if ((size_t) nchars < size) { /* The buffer was large enough */
|
2009-05-10 19:19:03 +05:30
|
|
|
/* readlink does not nul-terminate */
|
|
|
|
buffer[nchars] = '\0';
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try again with a bigger buffer */
|
|
|
|
free (buffer);
|
|
|
|
size *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_symlink - copy a symlink
|
|
|
|
*
|
|
|
|
* Copy a symlink from src to dst.
|
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* statp, mt, old_uid, new_uid, old_gid, and new_gid are used to set
|
|
|
|
* the access and modification and the access rights.
|
2007-12-28 06:05:41 +05:30
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_symlink (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
unused bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
2009-05-10 19:19:03 +05:30
|
|
|
char *oldlink;
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2009-04-25 05:11:28 +05:30
|
|
|
/* copy_tree () must be the entry point */
|
|
|
|
assert (NULL != src_orig);
|
|
|
|
assert (NULL != dst_orig);
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
/*
|
|
|
|
* Get the name of the file which the link points
|
|
|
|
* to. If that name begins with the original
|
|
|
|
* source directory name, that part of the link
|
|
|
|
* name will be replaced with the original
|
|
|
|
* destination directory name.
|
|
|
|
*/
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
oldlink = readlink_malloc (src->full_path);
|
2009-05-10 19:19:03 +05:30
|
|
|
if (NULL == oldlink) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
|
|
|
|
/* If src was a link to an entry of the src_orig directory itself,
|
|
|
|
* create a link to the corresponding entry in the dst_orig
|
|
|
|
* directory.
|
|
|
|
*/
|
2008-05-24 20:49:02 +05:30
|
|
|
if (strncmp (oldlink, src_orig, strlen (src_orig)) == 0) {
|
2009-05-10 19:19:03 +05:30
|
|
|
size_t len = strlen (dst_orig) + strlen (oldlink) - strlen (src_orig) + 1;
|
2010-08-22 18:19:07 +05:30
|
|
|
char *dummy = (char *) xmalloc (len);
|
|
|
|
(void) snprintf (dummy, len, "%s%s",
|
|
|
|
dst_orig,
|
|
|
|
oldlink + strlen (src_orig));
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
|
|
|
oldlink = dummy;
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
#ifdef WITH_SELINUX
|
2022-08-05 21:27:32 +05:30
|
|
|
if (set_selinux_file_context (dst->full_path, S_IFLNK) != 0) {
|
2010-08-22 18:19:07 +05:30
|
|
|
free (oldlink);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2022-08-05 21:27:32 +05:30
|
|
|
if ( (symlinkat (oldlink, dst->dirfd, dst->name) != 0)
|
|
|
|
|| (chownat_if_needed (dst, statp,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0)) {
|
2010-03-31 02:31:27 +05:30
|
|
|
/* FIXME: there are no modes on symlinks, right?
|
|
|
|
* ACL could be copied, but this would be much more
|
|
|
|
* complex than calling perm_copy_file.
|
|
|
|
* Ditto for Extended Attributes.
|
|
|
|
* We currently only document that ACL and Extended
|
|
|
|
* Attributes are not copied.
|
|
|
|
*/
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-05-10 19:19:03 +05:30
|
|
|
free (oldlink);
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if (utimensat (dst->dirfd, dst->name, mt, AT_SYMLINK_NOFOLLOW) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2009-05-10 19:19:03 +05:30
|
|
|
return 0;
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_hardlink - copy a hardlink
|
|
|
|
*
|
|
|
|
* Copy a hardlink from src to dst.
|
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_hardlink (const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
unused bool reset_selinux,
|
2007-12-28 04:53:51 +05:30
|
|
|
struct link_name *lp)
|
|
|
|
{
|
2010-03-31 04:44:04 +05:30
|
|
|
/* FIXME: selinux, ACL, Extended Attributes needed? */
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if (linkat (AT_FDCWD, lp->ln_name, dst->dirfd, dst->name, 0) != 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2009-04-25 05:11:28 +05:30
|
|
|
|
2007-12-28 05:53:33 +05:30
|
|
|
/* If the file could be unlinked, decrement the links counter,
|
2010-03-31 04:44:04 +05:30
|
|
|
* and forget about this link if it was the last reference */
|
2007-12-28 05:34:46 +05:30
|
|
|
lp->ln_count--;
|
|
|
|
if (lp->ln_count <= 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
remove_link (lp);
|
2007-12-28 05:10:00 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_special - copy a special file
|
|
|
|
*
|
|
|
|
* Copy a special file from src to dst.
|
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* statp, mt, old_uid, new_uid, old_gid, and new_gid are used to set
|
|
|
|
* the access and modification and the access rights.
|
2007-12-28 06:05:41 +05:30
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_special (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
#ifdef WITH_SELINUX
|
2022-08-05 21:27:32 +05:30
|
|
|
if (set_selinux_file_context (dst->full_path, statp->st_mode & S_IFMT) != 0) {
|
2010-08-22 18:19:07 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if ( (mknodat (dst->dirfd, dst->name, statp->st_mode & ~07777U, statp->st_rdev) != 0)
|
|
|
|
|| (chownat_if_needed (dst, statp,
|
2010-04-05 02:25:46 +05:30
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0)
|
2022-09-10 17:28:15 +05:30
|
|
|
|| (fchmodat (dst->dirfd, dst->name, statp->st_mode & 07777, AT_SYMLINK_NOFOLLOW) != 0)
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ACL
|
2022-08-05 21:27:32 +05:30
|
|
|
|| ( (perm_copy_path (src, dst, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2022-09-10 17:28:15 +05:30
|
|
|
#endif /* WITH_ACL */
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ATTR
|
|
|
|
/*
|
|
|
|
* If the third parameter is NULL, all extended attributes
|
|
|
|
* except those that define Access Control Lists are copied.
|
|
|
|
* ACLs are excluded by default because copying them between
|
|
|
|
* file systems with and without ACL support needs some
|
|
|
|
* additional logic so that no unexpected permissions result.
|
|
|
|
*/
|
2011-11-22 01:04:22 +05:30
|
|
|
|| ( !reset_selinux
|
2022-08-05 21:27:32 +05:30
|
|
|
&& (attr_copy_path (src, dst, NULL, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2010-03-31 02:31:27 +05:30
|
|
|
#endif /* WITH_ATTR */
|
2022-08-05 21:27:32 +05:30
|
|
|
|| (utimensat (dst->dirfd, dst->name, mt, AT_SYMLINK_NOFOLLOW) != 0)) {
|
2007-12-28 04:53:51 +05:30
|
|
|
err = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-08-05 21:27:27 +05:30
|
|
|
/*
|
|
|
|
* full_write - write entire buffer
|
|
|
|
*
|
|
|
|
* Write up to count bytes from the buffer starting at buf to the
|
|
|
|
* file referred to by the file descriptor fd.
|
|
|
|
* Retry in case of a short write.
|
|
|
|
*
|
|
|
|
* Returns the number of bytes written on success, -1 on error.
|
|
|
|
*/
|
|
|
|
static ssize_t full_write(int fd, const void *buf, size_t count) {
|
|
|
|
ssize_t written = 0;
|
|
|
|
|
|
|
|
while (count > 0) {
|
|
|
|
ssize_t res;
|
|
|
|
|
|
|
|
res = write(fd, buf, count);
|
|
|
|
if (res < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
written += res;
|
|
|
|
buf = (const unsigned char*)buf + res;
|
|
|
|
count -= (size_t)res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
2007-12-28 06:05:41 +05:30
|
|
|
/*
|
|
|
|
* copy_file - copy a file
|
|
|
|
*
|
|
|
|
* Copy a file from src to dst.
|
|
|
|
*
|
2010-04-05 02:25:46 +05:30
|
|
|
* statp, mt, old_uid, new_uid, old_gid, and new_gid are used to set
|
|
|
|
* the access and modification and the access rights.
|
2007-12-28 06:05:41 +05:30
|
|
|
*
|
|
|
|
* Return 0 on success, -1 on error.
|
|
|
|
*/
|
2022-08-05 21:27:32 +05:30
|
|
|
static int copy_file (const struct path_info *src, const struct path_info *dst,
|
2010-08-21 23:01:45 +05:30
|
|
|
bool reset_selinux,
|
2022-08-05 21:27:32 +05:30
|
|
|
const struct stat *statp, const struct timespec mt[],
|
2010-04-05 02:25:46 +05:30
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
2007-12-28 04:53:51 +05:30
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
int ifd;
|
|
|
|
int ofd;
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
ifd = openat (src->dirfd, src->name, O_RDONLY|O_NOFOLLOW|O_CLOEXEC);
|
2007-12-28 05:00:36 +05:30
|
|
|
if (ifd < 0) {
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#ifdef WITH_SELINUX
|
2022-08-05 21:27:32 +05:30
|
|
|
if (set_selinux_file_context (dst->full_path, S_IFREG) != 0) {
|
Fix covscan RESOURCE_LEAK
Error: RESOURCE_LEAK (CWE-772): [#def1]
shadow-4.8.1/lib/commonio.c:320: alloc_fn: Storage is returned from allocation function "fopen_set_perms".
shadow-4.8.1/lib/commonio.c:320: var_assign: Assigning: "bkfp" = storage returned from "fopen_set_perms(backup, "w", &sb)".
shadow-4.8.1/lib/commonio.c:329: noescape: Resource "bkfp" is not freed or pointed-to in "putc".
shadow-4.8.1/lib/commonio.c:334: noescape: Resource "bkfp" is not freed or pointed-to in "fflush".
shadow-4.8.1/lib/commonio.c:339: noescape: Resource "bkfp" is not freed or pointed-to in "fileno".
shadow-4.8.1/lib/commonio.c:342: leaked_storage: Variable "bkfp" going out of scope leaks the storage it points to.
340| || (fclose (bkfp) != 0)) {
341| /* FIXME: unlink the backup file? */
342|-> return -1;
343| }
344|
Error: RESOURCE_LEAK (CWE-772): [#def2]
shadow-4.8.1/libmisc/addgrps.c:69: alloc_fn: Storage is returned from allocation function "malloc".
shadow-4.8.1/libmisc/addgrps.c:69: var_assign: Assigning: "grouplist" = storage returned from "malloc(i * 4UL)".
shadow-4.8.1/libmisc/addgrps.c:73: noescape: Resource "grouplist" is not freed or pointed-to in "getgroups". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/libmisc/addgrps.c:126: leaked_storage: Variable "grouplist" going out of scope leaks the storage it points to.
124| }
125|
126|-> return 0;
127| }
128| #else /* HAVE_SETGROUPS && !USE_PAM */
Error: RESOURCE_LEAK (CWE-772): [#def3]
shadow-4.8.1/libmisc/chowntty.c:62: alloc_fn: Storage is returned from allocation function "getgr_nam_gid".
shadow-4.8.1/libmisc/chowntty.c:62: var_assign: Assigning: "grent" = storage returned from "getgr_nam_gid(getdef_str("TTYGROUP"))".
shadow-4.8.1/libmisc/chowntty.c:98: leaked_storage: Variable "grent" going out of scope leaks the storage it points to.
96| */
97| #endif
98|-> }
99|
Error: RESOURCE_LEAK (CWE-772): [#def4]
shadow-4.8.1/libmisc/copydir.c:742: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:742: var_assign: Assigning: "ifd" = handle returned from "open(src, 0)".
shadow-4.8.1/libmisc/copydir.c:748: leaked_handle: Handle variable "ifd" going out of scope leaks the handle.
746| #ifdef WITH_SELINUX
747| if (set_selinux_file_context (dst, NULL) != 0) {
748|-> return -1;
749| }
750| #endif /* WITH_SELINUX */
Error: RESOURCE_LEAK (CWE-772): [#def5]
shadow-4.8.1/libmisc/copydir.c:751: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:751: var_assign: Assigning: "ofd" = handle returned from "open(dst, 577, statp->st_mode & 0xfffU)".
shadow-4.8.1/libmisc/copydir.c:752: noescape: Resource "ofd" is not freed or pointed-to in "fchown_if_needed".
shadow-4.8.1/libmisc/copydir.c:775: leaked_handle: Handle variable "ofd" going out of scope leaks the handle.
773| ) {
774| (void) close (ifd);
775|-> return -1;
776| }
777|
Error: RESOURCE_LEAK (CWE-772): [#def7]
shadow-4.8.1/libmisc/idmapping.c:188: alloc_fn: Storage is returned from allocation function "xmalloc".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "buf" = storage returned from "xmalloc(bufsize)".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "pos" = "buf".
shadow-4.8.1/libmisc/idmapping.c:213: noescape: Resource "buf" is not freed or pointed-to in "write".
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "pos" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "buf" going out of scope leaks the storage it points to.
217| }
218| close(fd);
219|-> }
Error: RESOURCE_LEAK (CWE-772): [#def8]
shadow-4.8.1/libmisc/list.c:211: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/libmisc/list.c:211: var_assign: Assigning: "members" = storage returned from "xstrdup(comma)".
shadow-4.8.1/libmisc/list.c:217: var_assign: Assigning: "cp" = "members".
shadow-4.8.1/libmisc/list.c:218: noescape: Resource "cp" is not freed or pointed-to in "strchr".
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "cp" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "members" going out of scope leaks the storage it points to.
242| if ('\0' == *members) {
243| *array = (char *) 0;
244|-> return array;
245| }
246|
Error: RESOURCE_LEAK (CWE-772): [#def11]
shadow-4.8.1/libmisc/myname.c:61: alloc_fn: Storage is returned from allocation function "xgetpwnam".
shadow-4.8.1/libmisc/myname.c:61: var_assign: Assigning: "pw" = storage returned from "xgetpwnam(cp)".
shadow-4.8.1/libmisc/myname.c:67: leaked_storage: Variable "pw" going out of scope leaks the storage it points to.
65| }
66|
67|-> return xgetpwuid (ruid);
68| }
69|
Error: RESOURCE_LEAK (CWE-772): [#def12]
shadow-4.8.1/libmisc/user_busy.c:260: alloc_fn: Storage is returned from allocation function "opendir".
shadow-4.8.1/libmisc/user_busy.c:260: var_assign: Assigning: "task_dir" = storage returned from "opendir(task_path)".
shadow-4.8.1/libmisc/user_busy.c:262: noescape: Resource "task_dir" is not freed or pointed-to in "readdir".
shadow-4.8.1/libmisc/user_busy.c:278: leaked_storage: Variable "task_dir" going out of scope leaks the storage it points to.
276| _("%s: user %s is currently used by process %d\n"),
277| Prog, name, pid);
278|-> return 1;
279| }
280| }
Error: RESOURCE_LEAK (CWE-772): [#def20]
shadow-4.8.1/src/newgrp.c:162: alloc_fn: Storage is returned from allocation function "xgetspnam".
shadow-4.8.1/src/newgrp.c:162: var_assign: Assigning: "spwd" = storage returned from "xgetspnam(pwd->pw_name)".
shadow-4.8.1/src/newgrp.c:234: leaked_storage: Variable "spwd" going out of scope leaks the storage it points to.
232| }
233|
234|-> return;
235|
236| failure:
Error: RESOURCE_LEAK (CWE-772): [#def21]
shadow-4.8.1/src/passwd.c:530: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/src/passwd.c:530: var_assign: Assigning: "cp" = storage returned from "xstrdup(crypt_passwd)".
shadow-4.8.1/src/passwd.c:551: noescape: Resource "cp" is not freed or pointed-to in "strlen".
shadow-4.8.1/src/passwd.c:554: noescape: Resource "cp" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/src/passwd.c:555: overwrite_var: Overwriting "cp" in "cp = newpw" leaks the storage that "cp" points to.
553| strcpy (newpw, "!");
554| strcat (newpw, cp);
555|-> cp = newpw;
556| }
557| return cp;
2021-06-14 16:09:48 +05:30
|
|
|
(void) close (ifd);
|
2010-08-22 18:19:07 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2010-03-31 02:30:43 +05:30
|
|
|
#endif /* WITH_SELINUX */
|
2022-09-10 17:28:15 +05:30
|
|
|
ofd = openat (dst->dirfd, dst->name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, 0600);
|
2007-12-28 05:33:26 +05:30
|
|
|
if ( (ofd < 0)
|
2010-04-05 02:25:46 +05:30
|
|
|
|| (fchown_if_needed (ofd, statp,
|
|
|
|
old_uid, new_uid, old_gid, new_gid) != 0)
|
2022-09-10 17:28:15 +05:30
|
|
|
|| (fchmod (ofd, statp->st_mode & 07777) != 0)
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ACL
|
2022-08-05 21:27:32 +05:30
|
|
|
|| ( (perm_copy_fd (src->full_path, ifd, dst->full_path, ofd, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2022-09-10 17:28:15 +05:30
|
|
|
#endif /* WITH_ACL */
|
2010-03-31 02:31:27 +05:30
|
|
|
#ifdef WITH_ATTR
|
|
|
|
/*
|
|
|
|
* If the third parameter is NULL, all extended attributes
|
|
|
|
* except those that define Access Control Lists are copied.
|
|
|
|
* ACLs are excluded by default because copying them between
|
|
|
|
* file systems with and without ACL support needs some
|
|
|
|
* additional logic so that no unexpected permissions result.
|
|
|
|
*/
|
2011-11-22 01:04:22 +05:30
|
|
|
|| ( !reset_selinux
|
2022-08-05 21:27:32 +05:30
|
|
|
&& (attr_copy_fd (src->full_path, ifd, dst->full_path, ofd, NULL, &ctx) != 0)
|
2011-11-22 01:04:22 +05:30
|
|
|
&& (errno != 0))
|
2010-03-31 02:31:27 +05:30
|
|
|
#endif /* WITH_ATTR */
|
|
|
|
) {
|
Fix covscan RESOURCE_LEAK
Error: RESOURCE_LEAK (CWE-772): [#def1]
shadow-4.8.1/lib/commonio.c:320: alloc_fn: Storage is returned from allocation function "fopen_set_perms".
shadow-4.8.1/lib/commonio.c:320: var_assign: Assigning: "bkfp" = storage returned from "fopen_set_perms(backup, "w", &sb)".
shadow-4.8.1/lib/commonio.c:329: noescape: Resource "bkfp" is not freed or pointed-to in "putc".
shadow-4.8.1/lib/commonio.c:334: noescape: Resource "bkfp" is not freed or pointed-to in "fflush".
shadow-4.8.1/lib/commonio.c:339: noescape: Resource "bkfp" is not freed or pointed-to in "fileno".
shadow-4.8.1/lib/commonio.c:342: leaked_storage: Variable "bkfp" going out of scope leaks the storage it points to.
340| || (fclose (bkfp) != 0)) {
341| /* FIXME: unlink the backup file? */
342|-> return -1;
343| }
344|
Error: RESOURCE_LEAK (CWE-772): [#def2]
shadow-4.8.1/libmisc/addgrps.c:69: alloc_fn: Storage is returned from allocation function "malloc".
shadow-4.8.1/libmisc/addgrps.c:69: var_assign: Assigning: "grouplist" = storage returned from "malloc(i * 4UL)".
shadow-4.8.1/libmisc/addgrps.c:73: noescape: Resource "grouplist" is not freed or pointed-to in "getgroups". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/libmisc/addgrps.c:126: leaked_storage: Variable "grouplist" going out of scope leaks the storage it points to.
124| }
125|
126|-> return 0;
127| }
128| #else /* HAVE_SETGROUPS && !USE_PAM */
Error: RESOURCE_LEAK (CWE-772): [#def3]
shadow-4.8.1/libmisc/chowntty.c:62: alloc_fn: Storage is returned from allocation function "getgr_nam_gid".
shadow-4.8.1/libmisc/chowntty.c:62: var_assign: Assigning: "grent" = storage returned from "getgr_nam_gid(getdef_str("TTYGROUP"))".
shadow-4.8.1/libmisc/chowntty.c:98: leaked_storage: Variable "grent" going out of scope leaks the storage it points to.
96| */
97| #endif
98|-> }
99|
Error: RESOURCE_LEAK (CWE-772): [#def4]
shadow-4.8.1/libmisc/copydir.c:742: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:742: var_assign: Assigning: "ifd" = handle returned from "open(src, 0)".
shadow-4.8.1/libmisc/copydir.c:748: leaked_handle: Handle variable "ifd" going out of scope leaks the handle.
746| #ifdef WITH_SELINUX
747| if (set_selinux_file_context (dst, NULL) != 0) {
748|-> return -1;
749| }
750| #endif /* WITH_SELINUX */
Error: RESOURCE_LEAK (CWE-772): [#def5]
shadow-4.8.1/libmisc/copydir.c:751: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:751: var_assign: Assigning: "ofd" = handle returned from "open(dst, 577, statp->st_mode & 0xfffU)".
shadow-4.8.1/libmisc/copydir.c:752: noescape: Resource "ofd" is not freed or pointed-to in "fchown_if_needed".
shadow-4.8.1/libmisc/copydir.c:775: leaked_handle: Handle variable "ofd" going out of scope leaks the handle.
773| ) {
774| (void) close (ifd);
775|-> return -1;
776| }
777|
Error: RESOURCE_LEAK (CWE-772): [#def7]
shadow-4.8.1/libmisc/idmapping.c:188: alloc_fn: Storage is returned from allocation function "xmalloc".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "buf" = storage returned from "xmalloc(bufsize)".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "pos" = "buf".
shadow-4.8.1/libmisc/idmapping.c:213: noescape: Resource "buf" is not freed or pointed-to in "write".
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "pos" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "buf" going out of scope leaks the storage it points to.
217| }
218| close(fd);
219|-> }
Error: RESOURCE_LEAK (CWE-772): [#def8]
shadow-4.8.1/libmisc/list.c:211: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/libmisc/list.c:211: var_assign: Assigning: "members" = storage returned from "xstrdup(comma)".
shadow-4.8.1/libmisc/list.c:217: var_assign: Assigning: "cp" = "members".
shadow-4.8.1/libmisc/list.c:218: noescape: Resource "cp" is not freed or pointed-to in "strchr".
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "cp" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "members" going out of scope leaks the storage it points to.
242| if ('\0' == *members) {
243| *array = (char *) 0;
244|-> return array;
245| }
246|
Error: RESOURCE_LEAK (CWE-772): [#def11]
shadow-4.8.1/libmisc/myname.c:61: alloc_fn: Storage is returned from allocation function "xgetpwnam".
shadow-4.8.1/libmisc/myname.c:61: var_assign: Assigning: "pw" = storage returned from "xgetpwnam(cp)".
shadow-4.8.1/libmisc/myname.c:67: leaked_storage: Variable "pw" going out of scope leaks the storage it points to.
65| }
66|
67|-> return xgetpwuid (ruid);
68| }
69|
Error: RESOURCE_LEAK (CWE-772): [#def12]
shadow-4.8.1/libmisc/user_busy.c:260: alloc_fn: Storage is returned from allocation function "opendir".
shadow-4.8.1/libmisc/user_busy.c:260: var_assign: Assigning: "task_dir" = storage returned from "opendir(task_path)".
shadow-4.8.1/libmisc/user_busy.c:262: noescape: Resource "task_dir" is not freed or pointed-to in "readdir".
shadow-4.8.1/libmisc/user_busy.c:278: leaked_storage: Variable "task_dir" going out of scope leaks the storage it points to.
276| _("%s: user %s is currently used by process %d\n"),
277| Prog, name, pid);
278|-> return 1;
279| }
280| }
Error: RESOURCE_LEAK (CWE-772): [#def20]
shadow-4.8.1/src/newgrp.c:162: alloc_fn: Storage is returned from allocation function "xgetspnam".
shadow-4.8.1/src/newgrp.c:162: var_assign: Assigning: "spwd" = storage returned from "xgetspnam(pwd->pw_name)".
shadow-4.8.1/src/newgrp.c:234: leaked_storage: Variable "spwd" going out of scope leaks the storage it points to.
232| }
233|
234|-> return;
235|
236| failure:
Error: RESOURCE_LEAK (CWE-772): [#def21]
shadow-4.8.1/src/passwd.c:530: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/src/passwd.c:530: var_assign: Assigning: "cp" = storage returned from "xstrdup(crypt_passwd)".
shadow-4.8.1/src/passwd.c:551: noescape: Resource "cp" is not freed or pointed-to in "strlen".
shadow-4.8.1/src/passwd.c:554: noescape: Resource "cp" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/src/passwd.c:555: overwrite_var: Overwriting "cp" in "cp = newpw" leaks the storage that "cp" points to.
553| strcpy (newpw, "!");
554| strcat (newpw, cp);
555|-> cp = newpw;
556| }
557| return cp;
2021-06-14 16:09:48 +05:30
|
|
|
if (ofd >= 0) {
|
|
|
|
(void) close (ofd);
|
|
|
|
}
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) close (ifd);
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2022-08-05 21:27:27 +05:30
|
|
|
while (true) {
|
|
|
|
char buf[8192];
|
|
|
|
ssize_t cnt;
|
|
|
|
|
|
|
|
cnt = read (ifd, buf, sizeof buf);
|
|
|
|
if (cnt < 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void) close (ofd);
|
|
|
|
(void) close (ifd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (cnt == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (full_write (ofd, buf, (size_t)cnt) < 0) {
|
Fix covscan RESOURCE_LEAK
Error: RESOURCE_LEAK (CWE-772): [#def1]
shadow-4.8.1/lib/commonio.c:320: alloc_fn: Storage is returned from allocation function "fopen_set_perms".
shadow-4.8.1/lib/commonio.c:320: var_assign: Assigning: "bkfp" = storage returned from "fopen_set_perms(backup, "w", &sb)".
shadow-4.8.1/lib/commonio.c:329: noescape: Resource "bkfp" is not freed or pointed-to in "putc".
shadow-4.8.1/lib/commonio.c:334: noescape: Resource "bkfp" is not freed or pointed-to in "fflush".
shadow-4.8.1/lib/commonio.c:339: noescape: Resource "bkfp" is not freed or pointed-to in "fileno".
shadow-4.8.1/lib/commonio.c:342: leaked_storage: Variable "bkfp" going out of scope leaks the storage it points to.
340| || (fclose (bkfp) != 0)) {
341| /* FIXME: unlink the backup file? */
342|-> return -1;
343| }
344|
Error: RESOURCE_LEAK (CWE-772): [#def2]
shadow-4.8.1/libmisc/addgrps.c:69: alloc_fn: Storage is returned from allocation function "malloc".
shadow-4.8.1/libmisc/addgrps.c:69: var_assign: Assigning: "grouplist" = storage returned from "malloc(i * 4UL)".
shadow-4.8.1/libmisc/addgrps.c:73: noescape: Resource "grouplist" is not freed or pointed-to in "getgroups". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/libmisc/addgrps.c:126: leaked_storage: Variable "grouplist" going out of scope leaks the storage it points to.
124| }
125|
126|-> return 0;
127| }
128| #else /* HAVE_SETGROUPS && !USE_PAM */
Error: RESOURCE_LEAK (CWE-772): [#def3]
shadow-4.8.1/libmisc/chowntty.c:62: alloc_fn: Storage is returned from allocation function "getgr_nam_gid".
shadow-4.8.1/libmisc/chowntty.c:62: var_assign: Assigning: "grent" = storage returned from "getgr_nam_gid(getdef_str("TTYGROUP"))".
shadow-4.8.1/libmisc/chowntty.c:98: leaked_storage: Variable "grent" going out of scope leaks the storage it points to.
96| */
97| #endif
98|-> }
99|
Error: RESOURCE_LEAK (CWE-772): [#def4]
shadow-4.8.1/libmisc/copydir.c:742: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:742: var_assign: Assigning: "ifd" = handle returned from "open(src, 0)".
shadow-4.8.1/libmisc/copydir.c:748: leaked_handle: Handle variable "ifd" going out of scope leaks the handle.
746| #ifdef WITH_SELINUX
747| if (set_selinux_file_context (dst, NULL) != 0) {
748|-> return -1;
749| }
750| #endif /* WITH_SELINUX */
Error: RESOURCE_LEAK (CWE-772): [#def5]
shadow-4.8.1/libmisc/copydir.c:751: open_fn: Returning handle opened by "open". [Note: The source code implementation of the function has been overridden by a user model.]
shadow-4.8.1/libmisc/copydir.c:751: var_assign: Assigning: "ofd" = handle returned from "open(dst, 577, statp->st_mode & 0xfffU)".
shadow-4.8.1/libmisc/copydir.c:752: noescape: Resource "ofd" is not freed or pointed-to in "fchown_if_needed".
shadow-4.8.1/libmisc/copydir.c:775: leaked_handle: Handle variable "ofd" going out of scope leaks the handle.
773| ) {
774| (void) close (ifd);
775|-> return -1;
776| }
777|
Error: RESOURCE_LEAK (CWE-772): [#def7]
shadow-4.8.1/libmisc/idmapping.c:188: alloc_fn: Storage is returned from allocation function "xmalloc".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "buf" = storage returned from "xmalloc(bufsize)".
shadow-4.8.1/libmisc/idmapping.c:188: var_assign: Assigning: "pos" = "buf".
shadow-4.8.1/libmisc/idmapping.c:213: noescape: Resource "buf" is not freed or pointed-to in "write".
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "pos" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/idmapping.c:219: leaked_storage: Variable "buf" going out of scope leaks the storage it points to.
217| }
218| close(fd);
219|-> }
Error: RESOURCE_LEAK (CWE-772): [#def8]
shadow-4.8.1/libmisc/list.c:211: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/libmisc/list.c:211: var_assign: Assigning: "members" = storage returned from "xstrdup(comma)".
shadow-4.8.1/libmisc/list.c:217: var_assign: Assigning: "cp" = "members".
shadow-4.8.1/libmisc/list.c:218: noescape: Resource "cp" is not freed or pointed-to in "strchr".
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "cp" going out of scope leaks the storage it points to.
shadow-4.8.1/libmisc/list.c:244: leaked_storage: Variable "members" going out of scope leaks the storage it points to.
242| if ('\0' == *members) {
243| *array = (char *) 0;
244|-> return array;
245| }
246|
Error: RESOURCE_LEAK (CWE-772): [#def11]
shadow-4.8.1/libmisc/myname.c:61: alloc_fn: Storage is returned from allocation function "xgetpwnam".
shadow-4.8.1/libmisc/myname.c:61: var_assign: Assigning: "pw" = storage returned from "xgetpwnam(cp)".
shadow-4.8.1/libmisc/myname.c:67: leaked_storage: Variable "pw" going out of scope leaks the storage it points to.
65| }
66|
67|-> return xgetpwuid (ruid);
68| }
69|
Error: RESOURCE_LEAK (CWE-772): [#def12]
shadow-4.8.1/libmisc/user_busy.c:260: alloc_fn: Storage is returned from allocation function "opendir".
shadow-4.8.1/libmisc/user_busy.c:260: var_assign: Assigning: "task_dir" = storage returned from "opendir(task_path)".
shadow-4.8.1/libmisc/user_busy.c:262: noescape: Resource "task_dir" is not freed or pointed-to in "readdir".
shadow-4.8.1/libmisc/user_busy.c:278: leaked_storage: Variable "task_dir" going out of scope leaks the storage it points to.
276| _("%s: user %s is currently used by process %d\n"),
277| Prog, name, pid);
278|-> return 1;
279| }
280| }
Error: RESOURCE_LEAK (CWE-772): [#def20]
shadow-4.8.1/src/newgrp.c:162: alloc_fn: Storage is returned from allocation function "xgetspnam".
shadow-4.8.1/src/newgrp.c:162: var_assign: Assigning: "spwd" = storage returned from "xgetspnam(pwd->pw_name)".
shadow-4.8.1/src/newgrp.c:234: leaked_storage: Variable "spwd" going out of scope leaks the storage it points to.
232| }
233|
234|-> return;
235|
236| failure:
Error: RESOURCE_LEAK (CWE-772): [#def21]
shadow-4.8.1/src/passwd.c:530: alloc_fn: Storage is returned from allocation function "xstrdup".
shadow-4.8.1/src/passwd.c:530: var_assign: Assigning: "cp" = storage returned from "xstrdup(crypt_passwd)".
shadow-4.8.1/src/passwd.c:551: noescape: Resource "cp" is not freed or pointed-to in "strlen".
shadow-4.8.1/src/passwd.c:554: noescape: Resource "cp" is not freed or pointed-to in "strcat". [Note: The source code implementation of the function has been overridden by a builtin model.]
shadow-4.8.1/src/passwd.c:555: overwrite_var: Overwriting "cp" in "cp = newpw" leaks the storage that "cp" points to.
553| strcpy (newpw, "!");
554| strcat (newpw, cp);
555|-> cp = newpw;
556| }
557| return cp;
2021-06-14 16:09:48 +05:30
|
|
|
(void) close (ofd);
|
2010-03-23 14:26:52 +05:30
|
|
|
(void) close (ifd);
|
2007-12-28 04:53:51 +05:30
|
|
|
return -1;
|
2007-12-26 19:24:23 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
}
|
2007-12-26 19:24:23 +05:30
|
|
|
|
2008-05-24 20:49:02 +05:30
|
|
|
(void) close (ifd);
|
2007-12-28 04:53:51 +05:30
|
|
|
if (close (ofd) != 0) {
|
|
|
|
return -1;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-12-28 04:53:51 +05:30
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
if (utimensat (dst->dirfd, dst->name, mt, AT_SYMLINK_NOFOLLOW) != 0) {
|
2008-09-07 06:21:17 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-12-28 04:53:51 +05:30
|
|
|
return err;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
2010-04-05 02:25:46 +05:30
|
|
|
#define def_chown_if_needed(chown_function, type_dst) \
|
|
|
|
static int chown_function ## _if_needed (type_dst dst, \
|
|
|
|
const struct stat *statp, \
|
|
|
|
uid_t old_uid, uid_t new_uid, \
|
|
|
|
gid_t old_gid, gid_t new_gid) \
|
|
|
|
{ \
|
|
|
|
uid_t tmpuid = (uid_t) -1; \
|
|
|
|
gid_t tmpgid = (gid_t) -1; \
|
|
|
|
\
|
|
|
|
/* Use new_uid if old_uid is set to -1 or if the file was \
|
|
|
|
* owned by the user. */ \
|
|
|
|
if (((uid_t) -1 == old_uid) || (statp->st_uid == old_uid)) { \
|
|
|
|
tmpuid = new_uid; \
|
|
|
|
} \
|
|
|
|
/* Otherwise, or if new_uid was set to -1, we keep the same \
|
|
|
|
* owner. */ \
|
|
|
|
if ((uid_t) -1 == tmpuid) { \
|
|
|
|
tmpuid = statp->st_uid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (((gid_t) -1 == old_gid) || (statp->st_gid == old_gid)) { \
|
|
|
|
tmpgid = new_gid; \
|
|
|
|
} \
|
|
|
|
if ((gid_t) -1 == tmpgid) { \
|
|
|
|
tmpgid = statp->st_gid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
return chown_function (dst, tmpuid, tmpgid); \
|
|
|
|
}
|
|
|
|
|
|
|
|
def_chown_if_needed (fchown, int)
|
|
|
|
|
2022-08-05 21:27:32 +05:30
|
|
|
static int chownat_if_needed (const struct path_info *dst,
|
|
|
|
const struct stat *statp,
|
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
|
|
|
{
|
|
|
|
uid_t tmpuid = (uid_t) -1;
|
|
|
|
gid_t tmpgid = (gid_t) -1;
|
|
|
|
|
|
|
|
/* Use new_uid if old_uid is set to -1 or if the file was
|
|
|
|
* owned by the user. */
|
|
|
|
if (((uid_t) -1 == old_uid) || (statp->st_uid == old_uid)) {
|
|
|
|
tmpuid = new_uid;
|
|
|
|
}
|
|
|
|
/* Otherwise, or if new_uid was set to -1, we keep the same
|
|
|
|
* owner. */
|
|
|
|
if ((uid_t) -1 == tmpuid) {
|
|
|
|
tmpuid = statp->st_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((gid_t) -1 == old_gid) || (statp->st_gid == old_gid)) {
|
|
|
|
tmpgid = new_gid;
|
|
|
|
}
|
|
|
|
if ((gid_t) -1 == tmpgid) {
|
|
|
|
tmpgid = statp->st_gid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fchownat (dst->dirfd, dst->name, tmpuid, tmpgid, AT_SYMLINK_NOFOLLOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* copy_tree - copy files in a directory tree
|
|
|
|
*
|
|
|
|
* copy_tree() walks a directory tree and copies ordinary files
|
|
|
|
* as it goes.
|
|
|
|
*
|
|
|
|
* When reset_selinux is enabled, extended attributes (and thus
|
|
|
|
* SELinux attributes) are not copied.
|
|
|
|
*
|
|
|
|
* old_uid and new_uid are used to set the ownership of the copied
|
|
|
|
* files. Unless old_uid is set to -1, only the files owned by
|
|
|
|
* old_uid have their ownership changed to new_uid. In addition, if
|
|
|
|
* new_uid is set to -1, no ownership will be changed.
|
|
|
|
*
|
|
|
|
* The same logic applies for the group-ownership and
|
|
|
|
* old_gid/new_gid.
|
|
|
|
*/
|
|
|
|
int copy_tree (const char *src_root, const char *dst_root,
|
|
|
|
bool copy_root, bool reset_selinux,
|
|
|
|
uid_t old_uid, uid_t new_uid,
|
|
|
|
gid_t old_gid, gid_t new_gid)
|
|
|
|
{
|
|
|
|
const struct path_info src = {
|
|
|
|
.full_path = src_root,
|
|
|
|
.dirfd = AT_FDCWD,
|
|
|
|
.name = src_root
|
|
|
|
};
|
|
|
|
const struct path_info dst = {
|
|
|
|
.full_path = dst_root,
|
|
|
|
.dirfd = AT_FDCWD,
|
|
|
|
.name = dst_root
|
|
|
|
};
|
|
|
|
|
|
|
|
return copy_tree_impl(&src, &dst, copy_root, reset_selinux,
|
|
|
|
old_uid, new_uid, old_gid, new_gid);
|
|
|
|
}
|