Skip to contentSkip to conversation

makedevs: accept usernames and group names

GIT PULL New Yann E. MORIN 2 participants 6 messages Jan 21, 2016 – Feb 1, 2016 VFS

lore
Share & tools
lore
#0 lore raw

Hello All!

This series is an attempt to ad the possibility to use actual usernames and group names in the _PERMISSIONS and _DEVICES tables, and not be limitted to only numerical UIDs and GIDs.

The 4th and last patch in the series should *not* be applied, it is only provided as a simple test-bed to check this series.

Regards,
Yann E. MORIN.

The following changes since commit f557fcac7eeff0d2167852b44a8f596f12e4c9b4:

libsecret: bump to version 0.18.4 (2016-01-21 15:26:53 +0100)

are available in the git repository at:

git://git.busybox.net/~ymorin/git/buildroot yem/etc-passwd

for you to fetch changes up to 1091c86340c443e61aba2523f0eb9ef7a2881e6a:

[DO NOT COMMIT] busybox: user username and group name (2016-01-21 22:21:25 +0100)

----------------------------------------------------------------
Yann E. MORIN (4):
fs/common: generate users before setting permissions package/makedevs: use the rooted /etc/passwd and /etc/group docs/manual: makedev syntax accepts usernames and group names [DO NOT COMMIT] busybox: user username and group name

 docs/manual/adding-packages-generic.txt | 22 ++++++++++----------
 docs/manual/makedev-syntax.txt          | 20 ++++++++++--------
 fs/common.mk                            | 10 ++++-----
 package/busybox/busybox.mk              |  6 +++++-
 package/makedevs/makedevs.c             | 36 ++++++++++++++++++++++++++-------
 5 files changed, 62 insertions(+), 32 deletions(-)
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'
#1 lore raw

PATCH 1/4 fs/common: generate users before setting permissions

We will need the users and groups to get defined before we can use them from makedevs.

Cc Thomas Petazzoni <[email protected]>
---
 fs/common.mk | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/common.mk +5 -5
index 528e194..3a424f6 100644
@@ -78,6 +78,11 @@ $$(BINARIES_DIR)/rootfs.$(1): target-finalize $$(ROOTFS_$(2)_DEPENDENCIES)
 	rm -f $$(USERS_TABLE)
 	echo "set -e" >> $$(FAKEROOT_SCRIPT)
 	echo "chown -h -R 0:0 $$(TARGET_DIR)" >> $$(FAKEROOT_SCRIPT)
+ifneq ($$(ROOTFS_USERS_TABLES),)
+	cat $$(ROOTFS_USERS_TABLES) >> $$(USERS_TABLE)
+endif
+	printf '$$(subst $$(sep),\n,$$(PACKAGES_USERS))' >> $$(USERS_TABLE)
+	PATH=$$(BR_PATH) $$(TOPDIR)/support/scripts/mkusers $$(USERS_TABLE) $$(TARGET_DIR) >> $$(FAKEROOT_SCRIPT)
 ifneq ($$(ROOTFS_DEVICE_TABLES),)
 	cat $$(ROOTFS_DEVICE_TABLES) > $$(FULL_DEVICE_TABLE)
 ifeq ($$(BR2_ROOTFS_DEVICE_CREATION_STATIC),y)
@@ -86,11 +91,6 @@ endif
 	printf '$$(subst $$(sep),\n,$$(PACKAGES_PERMISSIONS_TABLE))' >> $$(FULL_DEVICE_TABLE)
 	echo "$$(HOST_DIR)/usr/bin/makedevs -d $$(FULL_DEVICE_TABLE) $$(TARGET_DIR)" >> $$(FAKEROOT_SCRIPT)
 endif
-ifneq ($$(ROOTFS_USERS_TABLES),)
-	cat $$(ROOTFS_USERS_TABLES) >> $$(USERS_TABLE)
-endif
-	printf '$$(subst $$(sep),\n,$$(PACKAGES_USERS))' >> $$(USERS_TABLE)
-	PATH=$$(BR_PATH) $$(TOPDIR)/support/scripts/mkusers $$(USERS_TABLE) $$(TARGET_DIR) >> $$(FAKEROOT_SCRIPT)
 	echo "$$(ROOTFS_$(2)_CMD)" >> $$(FAKEROOT_SCRIPT)
 	chmod a+x $$(FAKEROOT_SCRIPT)
 	PATH=$$(BR_PATH) $$(HOST_DIR)/usr/bin/fakeroot -- $$(FAKEROOT_SCRIPT)
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/4 package/makedevs: use the rooted /etc/passwd and /etc/group

In reply to Yann E. MORIN

Currently, makedevs will query the host's /etc/passwd and /etc/group to resolve usernames and group names. This is inherently flawed, as we can never guarantee that the UIDs will be the same on the target as on the host, or even whether a particular user does exist on the host.

This is because getpwnam() and getgrnam() will forcibly read the system's /etc/passwd and /etc/group, and there is no way to tell them to look anywhere else.

However, we can use use fgetpwent() and fgetgrent() instead, for which we can pass a FILE* stream to read from to get the entries. This means we must implement the scanning-loop ourselves, but fortunately, that's pretty trivial to do.

Cc Thomas Petazzoni <[email protected]>
---
 package/makedevs/makedevs.c | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)
package/makedevs/makedevs.c +29 -7
index 53ff6fe..91f72e4 100644
@@ -40,6 +40,8 @@ const char *bb_applet_name;
 uid_t recursive_uid;
 gid_t recursive_gid;
 unsigned int recursive_mode;
+#define PASSWD_PATH "etc/passwd"  /* MUST be relative */
+#define GROUP_PATH "etc/group"  /* MUST be relative */
 
 void bb_verror_msg(const char *s, va_list p)
 {
@@ -255,10 +257,20 @@ char *bb_get_chomped_line_from_file(FILE *file)
 long my_getpwnam(const char *name)
 {
 	struct passwd *myuser;
+	FILE *stream;
 
-	myuser  = getpwnam(name);
-	if (myuser==NULL)
-		bb_error_msg_and_die("unknown user name: %s", name);
+	stream = bb_xfopen(PASSWD_PATH, "r");
+	while(1) {
+		errno = 0;
+		myuser = fgetpwent(stream);
+		if (errno)
+			bb_error_msg_and_die(strerror(errno));
+		if (myuser==NULL)
+			bb_error_msg_and_die("unknown user name: %s", name);
+		if (!strcmp(name, myuser->pw_name))
+			break;
+	}
+	fclose(stream);
 
 	return myuser->pw_uid;
 }
@@ -266,12 +278,22 @@ long my_getpwnam(const char *name)
 long my_getgrnam(const char *name)
 {
 	struct group *mygroup;
+	FILE *stream;
 
-	mygroup  = getgrnam(name);
-	if (mygroup==NULL)
-		bb_error_msg_and_die("unknown group name: %s", name);
+	stream = bb_xfopen(GROUP_PATH, "r");
+	while(1) {
+		errno = 0;
+		mygroup = fgetgrent(stream);
+		if (errno)
+			bb_error_msg_and_die(strerror(errno));
+		if (mygroup==NULL)
+			bb_error_msg_and_die("unknown group name: %s", name);
+		if (!strcmp(name, mygroup->gr_name))
+			break;
+	}
+	fclose(stream);
 
-	return (mygroup->gr_gid);
+	return mygroup->gr_gid;
 }
 
 unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
Signature · 1 line
1.9.1
#4 lore raw

PATCH 3/4 docs/manual: makedev syntax accepts usernames and group names

In reply to Yann E. MORIN
Cc Thomas Petazzoni <[email protected]>
---
 docs/manual/adding-packages-generic.txt | 22 +++++++++++-----------
 docs/manual/makedev-syntax.txt          | 20 ++++++++++++--------
 2 files changed, 23 insertions(+), 19 deletions(-)
docs/manual/adding-packages-generic.txt +11 -11
index 1f9c7bb..b7817e3 100644
@@ -43,16 +43,16 @@ system is based on hand-written Makefiles or shell scripts.
 28:	$(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/foo.d
 29: endef
 30:
-31: define LIBFOO_DEVICES
-32:	/dev/foo  c  666  0  0	42  0  -  -  -
+31: define LIBFOO_USERS
+32:	foo -1 libfoo -1 * - - - LibFoo daemon
 33: endef
 34:
-35: define LIBFOO_PERMISSIONS
-36:	/bin/foo  f  4755  0  0	 -  -  -  -  -
+35: define LIBFOO_DEVICES
+36:	/dev/foo  c  666  0  0	42  0  -  -  -
 37: endef
 38:
-39: define LIBFOO_USERS
-40:	foo -1 libfoo -1 * - - - LibFoo daemon
+39: define LIBFOO_PERMISSIONS
+40:	/bin/foo  f  4755  foo  libfoo	 -  -  -  -  -
 41: endef
 42:
 43: $(eval $(generic-package))
@@ -142,15 +142,15 @@ All these steps rely on the +$(@D)+ variable, which
 contains the directory where the source code of the package has been
 extracted.
 
-On line 31..33, we define a device-node file used by this package
+On lines 31..43, we define a user that is used by this package (e.g.
+to run a daemon as non-root) (+LIBFOO_USERS+).
+
+On line 35..37, we define a device-node file used by this package
 (+LIBFOO_DEVICES+).
 
-On line 35..37, we define the permissions to set to specific files
+On line 39..41, we define the permissions to set to specific files
 installed by this package (+LIBFOO_PERMISSIONS+).
 
-On lines 39..41, we define a user that is used by this package (e.g.
-to run a daemon as non-root) (+LIBFOO_USERS+).
-
 Finally, on line 43, we call the +generic-package+ function, which
 generates, according to the variables defined previously, all the
 Makefile code necessary to make your package working.
docs/manual/makedev-syntax.txt +12 -8
index cae37f7..0bbc16e 100644
@@ -28,8 +28,10 @@ There are a few non-trivial blocks:
   * c: a character device file
   * b: a block device file
   * p: a named pipe
-- +mode+, +uid+ and +gid+ are the usual permissions settings (only
-  numerical values are allowed)
+- +mode+ are the usual permissions settings (only numerical values
+  are allowed)
+- +uid+ and +gid+ are the UID and GID to set on this file; can be
+  either numerical values or actual names
 - +major+ and +minor+ are here for device files, set to +-+ for other
   files
 - +start+, +inc+ and +count+ are for when you want to create a batch
@@ -37,19 +39,21 @@ There are a few non-trivial blocks:
   incrementing its counter by +inc+ until it reaches +count+
 
 Let's say you want to change the permissions of a given file; using
-this syntax, you will need to put:
+this syntax, you will need to write:
 
 ----
-/usr/bin/foobar f 644 0 0 - - - - -
+/usr/bin/foo f 755 0 0 - - - - -
+/usr/bin/bar f 755 root root - - - - -
+/data/buz f buz-user buz-group - - - - -
 ----
 
 Alternatively, if you want to change owner/permission of a directory
-recursively, you can put (to set UID to 123, GID to 456 and access
+recursively, you can write (to set UID to foo, GID to bar and access
 rights to rwxr-x--- for the directory /usr/share/myapp and all files
 and directories below it):
 
 ----
-/usr/share/myapp r 750 123 456 - - - - -
+/usr/share/myapp r 750 foo bar - - - - -
 ----
 
 On the other hand, if you want to create the device file +/dev/hda+
@@ -57,13 +61,13 @@ and the corresponding 15 files for the partitions, you will need for
 +/dev/hda+:
 
 ----
-/dev/hda b 640 0 0 3 0 0 0 -
+/dev/hda b 640 root root 3 0 0 0 -
 ----
 
 and then for device files corresponding to the partitions of
 +/dev/hda+, +/dev/hdaX+, +X+ ranging from 1 to 15:
 
 ----
-/dev/hda b 640 0 0 3 1 1 1 15
+/dev/hda b 640 root root 3 1 1 1 15
 ----
 
Signature · 1 line
1.9.1
#5 lore raw

PATCH 4/4 busybox: user username and group name

In reply to Yann E. MORIN

This commit is just a testbed from the newly-introduced possibility to use usernames and group names from the _PREMISSIONS and _DEVICES variables, instaed of only numerical values.

Using that defconfig:

    BR2_arm=y
    BR2_cortex_a7=y
    BR2_TOOLCHAIN_EXTERNAL=y

Extract the generated rootfs.tar (as root!) and check that busybox is owned by 762:763.

Explicitly not Signed-off-by me, as this SHOULD NOT BE COMMITTED.

---
 package/busybox/busybox.mk | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
package/busybox/busybox.mk +5 -1
index 7f6dbd6..eb670e8 100644
@@ -50,8 +50,12 @@ BUSYBOX_KCONFIG_FRAGMENT_FILES = $(call qstrip,$(BR2_PACKAGE_BUSYBOX_CONFIG_FRAG
 BUSYBOX_KCONFIG_EDITORS = menuconfig xconfig gconfig
 BUSYBOX_KCONFIG_OPTS = $(BUSYBOX_MAKE_OPTS)
 
+define BUSYBOX_USERS
+bbuser 762 bbgroup 763 * - - -
+endef
+
 define BUSYBOX_PERMISSIONS
-	/bin/busybox                     f 4755 0  0 - - - - -
+	/bin/busybox f 4755 bbuser bbgroup - - - - -
 endef
 
 # If mdev will be used for device creation enable it and copy S10mdev to /etc/init.d
Signature · 1 line
1.9.1
#3 lore raw

PATCH 2/4 package/makedevs: use the rooted /etc/passwd and /etc/group

In reply to Yann E. MORIN
Quoted 42 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Yann" == Yann E MORIN <[email protected]> writes:

Currently, makedevs will query the host's /etc/passwd and /etc/group to resolve usernames and group names. This is inherently flawed, as we can never guarantee that the UIDs will be the same on the target as on the host, or even whether a particular user does exist on the host.

This is because getpwnam() and getgrnam() will forcibly read the system's /etc/passwd and /etc/group, and there is no way to tell them to look anywhere else.

However, we can use use fgetpwent() and fgetgrent() instead, for which we can pass a FILE* stream to read from to get the entries. This means we must implement the scanning-loop ourselves, but fortunately, that's pretty trivial to do.

Cc Thomas Petazzoni <[email protected]>
---
 package/makedevs/makedevs.c | 36 +++++++++++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 7 deletions(-)
package/makedevs/makedevs.c +3 -1
index 53ff6fe..91f72e4 100644
@@ -40,6 +40,8 @@ const char *bb_applet_name;
 uid_t recursive_uid;
 gid_t recursive_gid;
 unsigned int recursive_mode;
+#define PASSWD_PATH "etc/passwd"  /* MUST be relative */
+#define GROUP_PATH "etc/group"  /* MUST be relative */
 
 void bb_verror_msg(const char *s, va_list p)
 {
@@ -255,10 +257,20 @@ char *bb_get_chomped_line_from_file(FILE *file)
 long my_getpwnam(const char *name)
 {
 	struct passwd *myuser;
+	FILE *stream;
 
-	myuser  = getpwnam(name);

package/makedevs/makedevs.c char *bb_get_chomped_line_from_file(FILE *file) · line 269

Quoted 10 lines
package/makedevs/makedevs.c +8 -2
-	if (myuser==NULL)
-		bb_error_msg_and_die("unknown user name: %s", name);
+	stream = bb_xfopen(PASSWD_PATH, "r");
+	while(1) {
+		errno = 0;
+		myuser = fgetpwent(stream);
+		if (errno)
+			bb_error_msg_and_die(strerror(errno));
+		if (myuser==NULL)
+			bb_error_msg_and_die("unknown user name: %s", name);

On my system atleast, fgetpwent() returns NULL AND sets error to ENOENT, so if you pass an unknown user/group name (E.G. you mistyped it), makedevs dies with:

makedevs: No such file or directory

Which is less than helpful. I've fixed it by swapping the errno and myuser check around. While I was at it, I also changed the strerror(errno) -> bb_perror_msg_and_die("fgetpwent"); so the user has some context if fgetpwent ever fails for some other reason (corrupt passwd file?).

(Most of) the rest of the file uses spaces around == so I've added that.

Committed series with that fixed, thanks.

Signature · 1 line
Bye, Peter Korsgaard