Bug 16422

Summary: newfs(8) always make root's / directory
Product: Base System Reporter: ichimura <ichimura>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Open ---    
Severity: Affects Only Me CC: grahamperrin
Priority: Normal Keywords: feature, needs-qa
Version: Unspecified   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description ichimura 2000-01-28 16:10:04 UTC
I want to use removable disk. And root mount it using "amd" command.
So I run "newfs" command.

But created / directry's owner is not me but root(uid 0).
group is wheel(gid 0), too.
And permition is always 755.
So I couldn't write my disk...

Is this FreeBSD's policy or Bug?

Fix: Is is patch for /usr/src/sbin/newfs/mkfs.c
It is very short.
How-To-Repeat: root# chmod 666 /dev/SOMEDISK*
root# su foo
foo%  newfs /dev/SOMEDISK
foo%  exit
root# mount /dev/SOMEDISK /SOMEWHERE
root# ls -ld /SOMEWHERE
drwxr-xr-x  2 root  wheel  512 Jan 29 00:45 /SOMEWHERE/
Comment 1 dd freebsd_committer freebsd_triage 2001-06-17 04:07:53 UTC
State Changed
From-To: open->closed

newfs shouldn't leave artifacts of the current environment lying around. 
Besides, you can always use chown after newfs/mount.  E-mail questions@ 
if you're not sure how to do that.
Comment 2 adrian 2002-01-17 01:18:44 UTC
Hi folks,

	I know this PR is closed, but the fundamental need is real.
Having newfs always produce a root:wheel owned directory is contrary to
user mounts (sysctl vfs.usermount=1) facility.

	We use user mounts to provide desktop users access to the local
devices (floppy and cd-rom).  The users presently either need root
privileges to chown a newfs'd floppy or then need to get one of our admins
to do it.  This need is required by the default ownership of the filesystem
root after newfs fnishes.

	The attached diffs make newfs's behavior more compatible with user
mounts.  To defeat this new behavior, the -R flag is provided.  If people
do not like this being the default, I'd still like to see the -R flag, but
with its logic reversed to enable the behavior.

	Adrian
--
[ adrian@ubergeeks.com ]


--- mkfs.c.orig	Fri Dec 21 23:33:36 2001
+++ mkfs.c	Wed Jan 16 19:16:01 2002
@@ -95,6 +95,7 @@
 extern struct stat mfs_mtstat;	/* stat prior to mount          */
 extern int	Nflag;		/* run mkfs without writing file system */
 extern int	Oflag;		/* format as an 4.3BSD file system */
+extern int	Rflag;		/* Always set the root directory root:wheel */
 extern int	Uflag;		/* enable soft updates for file system */
 extern int	fssize;		/* file system size */
 extern int	ntracks;	/* # tracks/cylinder */
@@ -1010,8 +1011,17 @@
 	 */
 	if (mfs)
 		node.di_mode = IFDIR | 01777;
-	else
-		node.di_mode = IFDIR | UMASK;
+	else {
+		if (Rflag || (getuid() == 0) ) {
+			node.di_mode = IFDIR | UMASK;
+		} else {
+			mode_t mask = umask(0);
+			umask(mask);
+			node.di_mode = IFDIR | (0777 & ~mask);
+			node.di_uid = getuid();
+			node.di_gid = getgid();
+		}
+	}
 	node.di_nlink = PREDEFDIR;
 	if (Oflag)
 		node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR);
--- newfs.8.orig	Fri Dec 21 23:33:36 2001
+++ newfs.8	Wed Jan 16 20:00:04 2002
@@ -41,7 +41,7 @@
 .Nd construct a new file system
 .Sh SYNOPSIS
 .Nm
-.Op Fl NOU
+.Op Fl NORU
 .Op Fl S Ar sector-size
 .Op Fl T Ar disktype
 .Op Fl a Ar maxcontig
@@ -101,7 +101,16 @@
 In fact, it need not even be special.)
 Typically the defaults are reasonable, however
 .Nm
-has numerous options to allow the defaults to be selectively overridden.
+has numerous options to allow the defaults to be selectively
+overridden.  The default owner and group of the root directory of
+the formatted device is the same as that of the user issuing the
+.Nm newfs
+command, and the default mode is modified by the user's umask
+setting, unless
+.Nm newfs
+is run by root, in which case
+.Fl R
+flag is assumed.
 .Pp
 .Nm Mount_mfs
 is used to build a file system in virtual memory and then mount it
@@ -148,6 +157,12 @@
 format filesystem.
 This options is primarily used to build root filesystems
 that can be understood by older boot ROMs.
+.It Fl R
+Use the traditional behavior when setting the owner, group and mode
+for the root directory on the newly formated device.  i.e. Set it
+to root and wheel with the mode u=rwx,go=rx, rather than using the
+current user's UID, GID and umask, if not running as root.  This
+option is enabled implicitly if the user is root.
 .It Fl T
 Use information for the specified disk from
 .Pa /etc/disktab
--- newfs.c.orig	Fri Dec 21 23:33:36 2001
+++ newfs.c	Wed Jan 16 18:35:46 2002
@@ -170,6 +170,7 @@
 struct stat mfs_mtstat;		/* stat prior to mount		*/
 int	Nflag;			/* run without writing file system */
 int	Oflag;			/* format as an 4.3BSD file system */
+int	Rflag;			/* Always set the root directory root:wheel */
 int	Uflag;			/* enable soft updates for file system */
 int	fssize;			/* file system size */
 int	ntracks = NTRACKS;	/* # tracks/cylinder */
@@ -250,7 +251,7 @@

 	opstring = mfs ?
 	    "NF:T:Ua:b:c:d:e:f:g:h:i:m:o:s:" :
-	    "NOS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
+	    "NORS:T:Ua:b:c:d:e:f:g:h:i:k:l:m:n:o:p:r:s:t:u:vx:";
 	while ((ch = getopt(argc, argv, opstring)) != -1)
 		switch (ch) {
 		case 'N':
@@ -258,6 +259,9 @@
 			break;
 		case 'O':
 			Oflag = 1;
+			break;
+		case 'R':
+			Rflag = 1;
 			break;
 		case 'S':
 			if ((sectorsize = atoi(optarg)) <= 0)
Comment 3 Alexander Best freebsd_committer freebsd_triage 2010-09-13 15:53:21 UTC
State Changed
From-To: closed->open

It's time to re-evaluate this request imo.
Comment 4 Eitan Adler freebsd_committer freebsd_triage 2017-12-31 07:59:27 UTC
For bugs matching the following criteria:

Status: In Progress Changed: (is less than) 2014-06-01

Reset to default assignee and clear in-progress tags.

Mail being skipped
Comment 5 Graham Perrin freebsd_committer freebsd_triage 2022-10-17 12:35:09 UTC
Keyword: 

    patch
or  patch-ready

– in lieu of summary line prefix: 

    [patch]

* bulk change for the keyword
* summary lines may be edited manually (not in bulk). 

Keyword descriptions and search interface: 

    <https://bugs.freebsd.org/bugzilla/describekeywords.cgi>