Skip to contentSkip to conversation

builddeb: Fix rootless build in setuid/setgid directory

PATCH In review Sven Joachim 2 participants 6 messages Oct 26, 2020 – Oct 29, 2020 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 builddeb: Fix rootless build in setuid/setgid directory

Building 5.10-rc1 in a setgid directory failed with the following error:

dpkg-deb: error: control directory has bad permissions 2755 (must be

Quoted 1 line

=0755 and <=0775)

When building with fakeroot, the earlier chown call would have removed the setgid bits, but in a rootless build they remain.

Fixes 3e8541803624 ("builddeb: Enable rootless builds")
Cc Guillem Jover <[email protected]>
---
 scripts/package/builddeb | 2 ++
 1 file changed, 2 insertions(+)
scripts/package/builddeb +2 -0
index 1b11f8993629..91a502bb97e8 100755
@@ -45,6 +45,8 @@ create_package() {
 	chmod -R go-w "$pdir"
 	# in case we are in a restrictive umask environment like 0077
 	chmod -R a+rX "$pdir"
+	# in case we build in a setuid/setgid directory
+	chmod -R ug-s "$pdir"
 
 	# Create the package
 	dpkg-gencontrol -p$pname -P"$pdir"

--
2.28.0

#1 lore raw

PATCH 2/2 builddeb: Consolidate consecutive chmod calls into one

No need to call chmod three times when it can do everything at once.

---
 scripts/package/builddeb | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
scripts/package/builddeb +1 -5
index 91a502bb97e8..81ec6414726c 100755
@@ -42,11 +42,7 @@ create_package() {
 	else
 		chown -R root:root "$pdir"
 	fi
-	chmod -R go-w "$pdir"
-	# in case we are in a restrictive umask environment like 0077
-	chmod -R a+rX "$pdir"
-	# in case we build in a setuid/setgid directory
-	chmod -R ug-s "$pdir"
+	chmod -R go-w,a+rX,ug-s "$pdir"
 
 	# Create the package
 	dpkg-gencontrol -p$pname -P"$pdir"

--
2.28.0

#2 lore raw

Re: [PATCH 2/2] builddeb: Consolidate consecutive chmod calls into one

Quoted 13 lines · Sven Joachim

No need to call chmod three times when it can do everything at once.

---
 scripts/package/builddeb | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
scripts/package/builddeb +0 -0
index 91a502bb97e8..81ec6414726c 100755

scripts/package/builddeb create_package() { · line 45

Quoted 9 lines
scripts/package/builddeb +1 -5
@@ -42,11 +42,7 @@ create_package() {
        else
                chown -R root:root "$pdir"
        fi
-       chmod -R go-w "$pdir"
-       # in case we are in a restrictive umask environment like 0077
-       chmod -R a+rX "$pdir"
-       # in case we build in a setuid/setgid directory
-       chmod -R ug-s "$pdir"
+       chmod -R go-w,a+rX,ug-s "$pdir"

You added the comment in 1/2, then
you are deleting it in this patch.

Could you keep the comments for clarification?

# a+rX in case we are in a restrictive umask environment like 0077

# ug-s in case we build in a setuid/setgid directory
chmod -R go-w,a+rX,ug-s "$pdir"
Quoted 5 lines
scripts/package/builddeb +0 -1
        # Create the package
        dpkg-gencontrol -p$pname -P"$pdir"
--

2.28.0

--
Best Regards
Masahiro Yamada

#3 lore raw

PATCH 1/1 builddeb: Consolidate consecutive chmod calls into one

No need to call chmod three times when it can do everything at once.

---
 scripts/package/builddeb | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
scripts/package/builddeb +3 -5
index 91a502bb97e8..6a100c449579 100755
@@ -42,11 +42,9 @@ create_package() {
 	else
 		chown -R root:root "$pdir"
 	fi
-	chmod -R go-w "$pdir"
-	# in case we are in a restrictive umask environment like 0077
-	chmod -R a+rX "$pdir"
-	# in case we build in a setuid/setgid directory
-	chmod -R ug-s "$pdir"
+	# a+rX in case we are in a restrictive umask environment like 0077
+	# ug-s in case we build in a setuid/setgid directory
+	chmod -R go-w,a+rX,ug-s "$pdir"
 
 	# Create the package
 	dpkg-gencontrol -p$pname -P"$pdir"

--
2.29.1

#4 lore raw
In reply to Sven Joachim
Quoted 10 lines · Sven Joachim

Building 5.10-rc1 in a setgid directory failed with the following error:

dpkg-deb: error: control directory has bad permissions 2755 (must be

Quoted 1 line

=0755 and <=0775)

When building with fakeroot, the earlier chown call would have removed the setgid bits, but in a rootless build they remain.

Applied to linux-kbuild. Thanks.

I agreed with "g-s" but was not sure about "u-s" because nothing is explained about setuid,
and the setuid bit against directories seems to have no effect.

It was interesting to read this article:
https://superuser.com/questions/471844/why-is-setuid-ignored-on-directories

Also, it is summarized in the wikipedia
https://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories

"The setuid permission set on a directory is ignored on most UNIX and Linux systems.[citation needed] However FreeBSD can be configured to interpret setuid in a manner similar to setgid, in which case it forces all files and sub-directories created in a directory to be owned by that directory's owner - a simple form of inheritance.[5] This is generally not needed on most systems derived from BSD, since by default directories are treated as if their setgid bit is always set, regardless of the actual value. As is stated in open(2), "When a new file is created it is given the group of the directory which contains it.""

After all, I am convinced that it would not hurt to do "u-s" although I have never tested kernel builds on FreeBSD.

Quoted 21 lines
Fixes 3e8541803624 ("builddeb: Enable rootless builds")
Cc Guillem Jover <[email protected]>
---
 scripts/package/builddeb | 2 ++
 1 file changed, 2 insertions(+)
scripts/package/builddeb +2 -1
index 1b11f8993629..91a502bb97e8 100755
@@ -45,6 +45,8 @@ create_package() {
        chmod -R go-w "$pdir"
        # in case we are in a restrictive umask environment like 0077
        chmod -R a+rX "$pdir"
+       # in case we build in a setuid/setgid directory
+       chmod -R ug-s "$pdir"
 
        # Create the package
        dpkg-gencontrol -p$pname -P"$pdir"
--
Quoted 2 lines

2.28.0

--
Best Regards

Masahiro Yamada

#5 lore raw
Quoted 6 lines · Masahiro Yamada
Quoted 5 lines · Sven Joachim

Building 5.10-rc1 in a setgid directory failed with the following error:

dpkg-deb: error: control directory has bad permissions 2755 (must be

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

=0755 and <=0775)

When building with fakeroot, the earlier chown call would have removed the setgid bits, but in a rootless build they remain.

Applied to linux-kbuild. Thanks.

I don't see it there, have you pushed it out yet?

Quoted 28 lines

I agreed with "g-s" but was not sure about "u-s" because nothing is explained about setuid,
and the setuid bit against directories seems to have no effect.

It was interesting to read this article:
https://superuser.com/questions/471844/why-is-setuid-ignored-on-directories

Also, it is summarized in the wikipedia
https://en.wikipedia.org/wiki/Setuid#setuid_and_setgid_on_directories

"The setuid permission set on a directory is ignored on most UNIX and Linux systems.[citation needed] However FreeBSD can be configured to interpret setuid in a manner similar to setgid, in which case it forces all files and sub-directories created in a directory to be owned by that directory's owner - a simple form of inheritance.[5] This is generally not needed on most systems derived from BSD, since by default directories are treated as if their setgid bit is always set, regardless of the actual value. As is stated in open(2), "When a new file is created it is given the group of the directory which contains it.""

Quoted 2 lines

After all, I am convinced that it would not hurt to do "u-s" although I have never tested kernel builds on FreeBSD.

Agreed, setuid directories should not end up in the .deb files even if that bit does currently nothing.

Cheers,
Sven