Merge remote-tracking branch 'freebsd/stable/12' into hardened/12-stable/master

This commit is contained in:
HardenedBSD Sync Service 2022-04-25 00:01:15 -04:00
commit 3ede280ffe
5 changed files with 116 additions and 14 deletions

View File

@ -32,7 +32,7 @@
.\" @(#)cp.1 8.3 (Berkeley) 4/18/94
.\" $FreeBSD$
.\"
.Dd June 6, 2015
.Dd February 23, 2022
.Dt CP 1
.Os
.Sh NAME
@ -55,6 +55,14 @@
.Op Fl f | i | n
.Op Fl alpsvx
.Ar source_file ... target_directory
.Nm
.Op Fl f | i | n
.Op Fl alPpsvx
.Ar source_file target_file
.Nm
.Op Fl f | i | n
.Op Fl alPpsvx
.Ar source_file ... target_directory
.Sh DESCRIPTION
In the first synopsis form, the
.Nm
@ -84,10 +92,10 @@ If the
.Fl R
option is specified, all symbolic links are followed.
.It Fl P
If the
No symbolic links are followed.
This is the default if the
.Fl R
option is specified, no symbolic links are followed.
This is the default.
option is specified.
.It Fl R
If
.Ar source_file

View File

@ -87,7 +87,7 @@ static char emptystring[] = "";
PATH_T to = { to.p_path, emptystring, "" };
int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
static int Rflag, rflag;
static int Hflag, Lflag, Rflag, rflag;
volatile sig_atomic_t info;
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
@ -100,22 +100,23 @@ main(int argc, char *argv[])
{
struct stat to_stat, tmp_stat;
enum op type;
int Hflag, Lflag, ch, fts_options, r, have_trailing_slash;
int Pflag, ch, fts_options, r, have_trailing_slash;
char *target;
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
Hflag = Lflag = 0;
Pflag = 0;
while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
Lflag = 0;
Lflag = Pflag = 0;
break;
case 'L':
Lflag = 1;
Hflag = 0;
Hflag = Pflag = 0;
break;
case 'P':
Pflag = 1;
Hflag = Lflag = 0;
break;
case 'R':
@ -124,6 +125,7 @@ main(int argc, char *argv[])
case 'a':
pflag = 1;
Rflag = 1;
Pflag = 1;
Hflag = Lflag = 0;
break;
case 'f':
@ -146,7 +148,7 @@ main(int argc, char *argv[])
break;
case 'r':
rflag = Lflag = 1;
Hflag = 0;
Hflag = Pflag = 0;
break;
case 's':
sflag = 1;
@ -180,7 +182,7 @@ main(int argc, char *argv[])
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL;
}
} else {
} else if (!Pflag) {
fts_options &= ~FTS_PHYSICAL;
fts_options |= FTS_LOGICAL | FTS_COMFOLLOW;
}
@ -271,6 +273,22 @@ main(int argc, char *argv[])
&to_stat)));
}
/* Does the right thing based on -R + -H/-L/-P */
static int
copy_stat(const char *path, struct stat *sb)
{
/*
* For -R -H/-P, we need to lstat() instead; copy() cares about the link
* itself rather than the target if we're not following links during the
* traversal.
*/
if (!Rflag || Lflag)
return (stat(path, sb));
return (lstat(path, sb));
}
static int
copy(char *argv[], enum op type, int fts_options, struct stat *root_stat)
{
@ -448,7 +466,7 @@ copy(char *argv[], enum op type, int fts_options, struct stat *root_stat)
}
/* Not an error but need to remember it happened. */
if (stat(to.p_path, &to_stat) == -1)
if (copy_stat(to.p_path, &to_stat) == -1)
dne = 1;
else {
if (to_stat.st_dev == curr->fts_statp->st_dev &&

View File

@ -43,6 +43,21 @@ basic_body()
check_size baz 4
}
atf_test_case basic_symlink
basic_symlink_body()
{
echo "foo" > bar
ln -s bar baz
atf_check cp baz foo
atf_check test '!' -L foo
atf_check -e inline:"cp: baz and baz are identical (not copied).\n" \
-s exit:1 cp baz baz
atf_check -e inline:"cp: bar and baz are identical (not copied).\n" \
-s exit:1 cp baz bar
}
atf_test_case chrdev
chrdev_body()
{
@ -139,12 +154,72 @@ matching_srctgt_nonexistent_body()
atf_check -e not-empty -s not-exit:0 stat foo/dne/foo
}
recursive_link_setup()
{
extra_cpflag=$1
mkdir -p foo/bar
ln -s bar foo/baz
mkdir foo-mirror
eval "cp -R $extra_cpflag foo foo-mirror"
}
atf_test_case recursive_link_dflt
recursive_link_dflt_body()
{
recursive_link_setup
# -P is the default, so this should work and preserve the link.
atf_check cp -R foo foo-mirror
atf_check test -L foo-mirror/foo/baz
}
atf_test_case recursive_link_Hflag
recursive_link_Hflag_body()
{
recursive_link_setup
# -H will not follow either, so this should also work and preserve the
# link.
atf_check cp -RH foo foo-mirror
atf_check test -L foo-mirror/foo/baz
}
atf_test_case recursive_link_Lflag
recursive_link_Lflag_body()
{
recursive_link_setup -L
# -L will work, but foo/baz ends up expanded to a directory.
atf_check test -d foo-mirror/foo/baz -a \
'(' ! -L foo-mirror/foo/baz ')'
atf_check cp -RL foo foo-mirror
atf_check test -d foo-mirror/foo/baz -a \
'(' ! -L foo-mirror/foo/baz ')'
}
atf_test_case standalone_Pflag
standalone_Pflag_body()
{
echo "foo" > bar
ln -s bar foo
atf_check cp -P foo baz
atf_check -o inline:'Symbolic Link\n' stat -f %SHT baz
}
atf_init_test_cases()
{
atf_add_test_case basic
atf_add_test_case basic_symlink
atf_add_test_case chrdev
atf_add_test_case matching_srctgt
atf_add_test_case matching_srctgt_contained
atf_add_test_case matching_srctgt_link
atf_add_test_case matching_srctgt_nonexistent
atf_add_test_case recursive_link_dflt
atf_add_test_case recursive_link_Hflag
atf_add_test_case recursive_link_Lflag
atf_add_test_case standalone_Pflag
}

View File

@ -873,7 +873,7 @@ main(int ac, char **av)
}
break;
case 'V':
if (options.version_addendum &&
if (options.version_addendum != NULL &&
*options.version_addendum != '\0')
fprintf(stderr, "%s %s, %s\n", SSH_RELEASE,
options.version_addendum,

View File

@ -924,7 +924,8 @@ drop_connection(int sock, int startups, int notify_pipe)
static void
usage(void)
{
if (options.version_addendum && *options.version_addendum != '\0')
if (options.version_addendum != NULL &&
*options.version_addendum != '\0')
fprintf(stderr, "%s %s, %s\n",
SSH_RELEASE,
options.version_addendum, OPENSSL_VERSION_STRING);