1830308 Members
2560 Online
110000 Solutions
New Discussion

Weird behavior in cp -p

 
Allen Wong_1
Occasional Contributor

Weird behavior in cp -p

Non-root users are able to create files owned by root. I am able to reproduce this on another HP-UX system, but not on Linux, BSD or Solaris.

$ uname -a
HP-UX Impala B.11.11 U 9000/800 1982107816 unlimited-user license
$ id
uid=1025(sos) gid=4(adm)
$ ls -l /sbin/init.d/tsm_client
-rwxr--r-- 1 root sys 3414 Dec 9 09:51 /sbin/init.d/tsm_client
$ cp -p /sbin/init.d/tsm_client .
$ ls -l tsm_client
-rwxr--r-- 1 root sys 3414 Dec 9 09:51 tsm_client
3 REPLIES 3
Patrick Wallek
Honored Contributor

Re: Weird behavior in cp -p

This isn't altogether unexpected.

The normal behavior of '-p' is to preserve permissions when copying a file.

You could also reproduce this manually.

As a normal user you can do:

$ touch afile2
$ ll afile2
-rw-r--r-- 1 wallekp users 0 Jan 20 16:24 afile2
$ chmod 744 afile2
$ ll afile2
-rwxr--r-- 1 wallekp users 0 Jan 20 16:24 afile2
$ chown root:sys afile2
$ ll afile2
-rwxr--r-- 1 root sys 0 Jan 20 16:24 afile2

But once you change the ownership of the file to someone other than yourself, you can't do anything else as shown below.

$ chown wallekp afile2
afile2: Not owner
A. Clay Stephenson
Acclaimed Contributor

Re: Weird behavior in cp -p

If you do a man cp, you will find that -p preserves mtime, atime, mode, uid, and gid as allowed by permissions. This is normal behavior. However, the dangerous mode bits (setuid, setgid, sticky-bit) are not preserved and are automatically cleared on the copy.
If it ain't broke, I can fix that.
Mic V.
Esteemed Contributor

Re: Weird behavior in cp -p

As I recall, the ability to hand off your files to another user is one of the historical differences between SysV(R4) and Berkeley. *BSD is obviously Berkeley-based. SunOS/Solaris 1.X was. I suspect Linux is but don't really know.

I also seem to recall you can change HP-UX chown behavior with (I think) the setprivgrp command ("CHOWN" priv; have to edit a boot script to make it global/"permanent"). Don't know what it does, if anything, to cp behavior.

Mic
What kind of a name is 'Wolverine'?