Skip to contentSkip to conversation

mkswap: Add warnings for insecure device permissions/owners

PATCH In review Wayne R. Roth 6 participants 18 messages Jan 19, 2016 – Jan 26, 2016 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mkswap: Add warnings for insecure device permissions/owners

Logic copied from sys-utils/swapon.c

---
 disk-utils/mkswap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
disk-utils/mkswap.c +10 -1
index c559e60..2729596 100644
@@ -344,7 +344,7 @@ static void write_header_to_device(struct mkswap_control *ctl)
 int main(int argc, char **argv)
 {
 	struct mkswap_control ctl = { .fd = -1 };
-	int c;
+	int c, permMask;
 	uint64_t sz;
 	int version = SWAP_VERSION;
 	char *block_count = NULL, *strsz = NULL;
@@ -464,6 +464,15 @@ int main(int argc, char **argv)
 			ctl.devname);
 
 	open_device(&ctl);
+        permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;
+        if ((ctl.devstat.st_mode & permMask) != 0)
+                warnx(_("%s: insecure permissions %04o, %04o suggested."),
+                                ctl.devname, ctl.devstat.st_mode & 07777,
+                                ~permMask & 0666);
+        if (S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)
+                warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
+                                ctl.devname, ctl.devstat.st_uid);
+
 
 	if (ctl.check)
 		check_blocks(&ctl);
Signature · 1 line
1.8.3.1
#1 lore raw

disk-utils/mkswap.c int main(int argc, char **argv) · line 474

Quoted 8 lines · Wayne R. Roth
disk-utils/mkswap.c +8 -0
+        permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;
+        if ((ctl.devstat.st_mode & permMask) != 0)
+                warnx(_("%s: insecure permissions %04o, %04o suggested."),
+                                ctl.devname, ctl.devstat.st_mode & 07777,
+                                ~permMask & 0666);
+        if (S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)
+                warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
+                                ctl.devname, ctl.devstat.st_uid);

i haven't read/tested the code, so my assumptions might be off, but this seems to complain even when creating files as non-root. mkswap should not do that. a perfectly reasonable use case is to create images as a non-root user for use with something like qemu.

maybe you want to add a getuid() check in there, or scuttle it altogether. -mike

#2 lore raw

PATCH 1/1 mkswap: Add warnings for insecure device permissions/owners Logic modified from sys-utils/swapon.c

From: "Wayne R. Roth" <[email protected]>

---
 disk-utils/mkswap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
disk-utils/mkswap.c +10 -1
index c559e60..0a8dc05 100644
@@ -344,7 +344,7 @@ static void write_header_to_device(struct mkswap_control *ctl)
 int main(int argc, char **argv)
 {
 	struct mkswap_control ctl = { .fd = -1 };
-	int c;
+	int c, permMask;
 	uint64_t sz;
 	int version = SWAP_VERSION;
 	char *block_count = NULL, *strsz = NULL;
@@ -464,6 +464,15 @@ int main(int argc, char **argv)
 			ctl.devname);
 
 	open_device(&ctl);
+        permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;
+        if ((ctl.devstat.st_mode & permMask) != 0)
+                warnx(_("%s: insecure permissions %04o, %04o suggested."),
+                                ctl.devname, ctl.devstat.st_mode & 07777,
+                                ~permMask & 0666);
+        if (getuid()==0 && S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)
+                warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
+                                ctl.devname, ctl.devstat.st_uid);
+
 
 	if (ctl.check)
 		check_blocks(&ctl);
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH] mkswap: Add warnings for insecure device permissions/owners Logic modified from sys-utils/swapon.c

Quoted 1 line · Wayne R. Roth

+ permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;

pretty sure you want to indent w/tabs, not spaces

Quoted 1 line

+ if (getuid()==0 && S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)

there should be spaces around the ==
-mike

#4 lore raw

PATCH 1/1 mkswap: add warnings for insecure device permissions/owners Logic modified from sys-utils/swapon.c

From: "Wayne R. Roth" <[email protected]>

---
 disk-utils/mkswap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
disk-utils/mkswap.c +10 -1
index c559e60..22999da 100644
@@ -344,7 +344,7 @@ static void write_header_to_device(struct mkswap_control *ctl)
 int main(int argc, char **argv)
 {
 	struct mkswap_control ctl = { .fd = -1 };
-	int c;
+	int c, permMask;
 	uint64_t sz;
 	int version = SWAP_VERSION;
 	char *block_count = NULL, *strsz = NULL;
@@ -464,6 +464,15 @@ int main(int argc, char **argv)
 			ctl.devname);
 
 	open_device(&ctl);
+	permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;
+	if ((ctl.devstat.st_mode & permMask) != 0)
+		warnx(_("%s: insecure permissions %04o, %04o suggested."),
+			ctl.devname, ctl.devstat.st_mode & 07777,
+			~permMask & 0666);
+	if (getuid() == 0 && S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)
+		warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
+			ctl.devname, ctl.devstat.st_uid);
+
 
 	if (ctl.check)
 		check_blocks(&ctl);
Signature · 1 line
1.8.3.1
#6 lore raw
In reply to Mike Frysinger
Quoted 10 lines · Mike Frysinger

disk-utils/mkswap.c int main(int argc, char **argv) · line 474

Quoted 8 lines · Wayne R. Roth
disk-utils/mkswap.c +8 -0
+        permMask = S_ISBLK(ctl.devstat.st_mode) ? 07007 : 07077;
+        if ((ctl.devstat.st_mode & permMask) != 0)
+                warnx(_("%s: insecure permissions %04o, %04o suggested."),
+                                ctl.devname, ctl.devstat.st_mode & 07777,
+                                ~permMask & 0666);
+        if (S_ISREG(ctl.devstat.st_mode) && ctl.devstat.st_uid != 0)
+                warnx(_("%s: insecure file owner %d, 0 (root) suggested."),
+                                ctl.devname, ctl.devstat.st_uid);
Quoted 6 lines

i haven't read/tested the code, so my assumptions might be off, but this seems to complain even when creating files as non-root. mkswap should not do that. a perfectly reasonable use case is to create images as a non-root user for use with something like qemu.

maybe you want to add a getuid() check in there, or scuttle it altogether.

I do not think mkswap, or mkfs of any sort, is an operation only root is allowed or expected to run. Taking device(s) in use is different matter, and that is already covered in swapon(8) and mount(8).

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#8 lore raw
Quoted 7 lines · Karel Zak
Quoted 1 line · Wayne R. Roth

Logic copied from sys-utils/swapon.c

Why? I think swapon is the right place for this check.

Karel

Hi Karel,

Warnings are probably best put in *both* mkswap and swapon for the following two reasons:

1. The person(s) reviewing the output for swapon may not be the same person(s) reviewing the output for mkswap. For example, this might happen with a company with a separate development and quality assurance department.

2. To my best knowledge the release of mkswap and swapon do not have to match. An example of when this might happen is the build process for an embedded device or virtual machine. I am pretty sure busybox does not warn on world readable swap right now.

This patch does not break any existing behavior. The worst case possibility from accepting this patch is it will annoy some people, and best case it will save millions of devices from being shipped with insecure permissions.

Thanks, Sarah

#9 lore raw

Am 21.01.2016 um 23:19 schrieb Sarah Newman:

Quoted 2 lines

This patch does not break any existing behavior. The worst case possibility from accepting this patch is it will annoy some people, and best case it will save millions of devices from being shipped with insecure permissions.

The worst case is it will train millions of administrators to ignore warning messages.

Signature · 3 lines
Tilman Schmidt                              E-Mail: [email protected]
Bonn, Germany
Nous, on a des fleurs et des bougies pour nous protéger.
#10 lore raw
Quoted 7 lines · Tilman Schmidt

Am 21.01.2016 um 23:19 schrieb Sarah Newman:

Quoted 2 lines

This patch does not break any existing behavior. The worst case possibility from accepting this patch is it will annoy some people, and best case it will save millions of devices from being shipped with insecure permissions.

The worst case is it will train millions of administrators to ignore warning messages.

If the warnings in swapon are legitimate, they are just as legitimate in mkswap if the file owner check is only done when mkswap is run as root.

Regarding the legitimacy of the swapon warnings: do you honestly believe most of the people who will get these warnings will have intended to have world readable swap or swap owned as a non-root owner?

When I search for "linux swap file" on google this is the second hit for me, the first being an arch linux wiki page:
https://www.linux.com/news/software/applications/8208-all-about-linux-swap-space "centos swap file" top two hits
https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-swap-creating-file.html
https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-swap-adding.html

I followed the instructions for CentOS on a CentOS 5 machine and it resulted in world readable swap. Those instructions came from Red Hat. If documentation from Red Hat gets it wrong, it's presumably a very common error. I made this mistake myself and I knew better.

--Sarah

#11 lore raw
Quoted 14 lines · Sarah Newman
Quoted 7 lines · Tilman Schmidt

Am 21.01.2016 um 23:19 schrieb Sarah Newman:

Quoted 2 lines

This patch does not break any existing behavior. The worst case possibility from accepting this patch is it will annoy some people, and best case it will save millions of devices from being shipped with insecure permissions.

The worst case is it will train millions of administrators to ignore warning messages.

If the warnings in swapon are legitimate, they are just as legitimate in mkswap if the file owner check is only done when mkswap is run as root.

Regarding the legitimacy of the swapon warnings: do you honestly believe most of the people who will get these warnings will have intended to have world readable swap or swap owned as a non-root owner?

Quoted 7 lines

When I search for "linux swap file" on google this is the second hit for me, the first being an arch linux wiki page:
https://www.linux.com/news/software/applications/8208-all-about-linux-swap-space "centos swap file" top two hits
https://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-swap-creating-file.html
https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-swap-adding.html

I followed the instructions for CentOS on a CentOS 5 machine and it resulted in world readable swap. Those instructions came from Red Hat. If documentation from Red Hat gets it wrong, it's presumably a very common error. I made this mistake myself and I knew better.

Fair points, but should same logic be applied to file holes when running mkswap? If the test _must_ be the same surely the mkswap & swapon should share the function(s) performing them, as drifting would be bad. Should these messages be optional, and if so which way around default ought to be? This will lead to --quite or --verbose options.

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
maintainer
#12 lore raw
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#13 lore raw
Quoted 9 lines · Karel Zak
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Why not completely optional?

$ swapon --path-permissions [ignore|complain|stop|fix]

Current default is 'complain', and it feels about right.

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#14 lore raw
Quoted 11 lines · Sami Kerola
Quoted 9 lines · Karel Zak
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Quoted 5 lines

Why not completely optional?

$ swapon --path-permissions [ignore|complain|stop|fix]

Current default is 'complain', and it feels about right.

Something like this.

https://github.com/kerolasa/lelux-utiliteetit/commit/d79b3cdbe4e61b7e10d595dec4d4e299c8300c9e

Notice that the change above is wrote on top of another change, that added swapon control structure.

https://github.com/kerolasa/lelux-utiliteetit/commit/4d510f769de438092558f51430f3b195f328ef0e

Signature · 2 lines
Sami Kerola
http://www.iki.fi/kerolasa/
#17 lore raw
In reply to Karel Zak
Quoted 9 lines · Karel Zak
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Rejecting insecure permissions in swapon will make warnings in mkswap even more important. Are there any further changes required to Wayne's latest patch to mkswap.c https://marc.info/?l=util-linux-ng&m=145327019508709&w=2 before it is merged?

Thanks, Sarah

maintainer Applied
#5 lore raw

Re: [PATCH] mkswap: add warnings for insecure device permissions/owners Logic modified from sys-utils/swapon.c

In reply to Wayne R. Roth
Quoted 2 lines · Wayne R. Roth
 disk-utils/mkswap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

Applied, although I don't feel 100% happy with it. At least we will educate users about the problem :-)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer
#15 lore raw
In reply to Sami Kerola
Quoted 6 lines · Sami Kerola
Quoted 5 lines · Karel Zak
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Quoted 8 lines
Quoted 4 lines

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Why not completely optional?

$ swapon --path-permissions [ignore|complain|stop|fix]

I don't think we want to merge another functionality to swapon. The warnings are enough. For the rest we have ch{own,mod}.

Let's Keep It Simple and Stupid. We all love kisses, right? :-)

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#16 lore raw
Quoted 12 lines · Karel Zak
Quoted 11 lines · Sami Kerola
Quoted 9 lines · Karel Zak
Quoted 3 lines · Sami Kerola

Alternatively one could make swapon to get rid of all permission bits and set ownership to UID 0 by default when ever it activates a swapfile. How about that.

Not sure if want to change any permissions on the fly, it would be better to reject files (by swapon) with insecure permissions and require something like --force for crazy users who wants to ignore this problem.

Quoted 8 lines
Quoted 3 lines

Why not completely optional?

$ swapon --path-permissions [ignore|complain|stop|fix]

I don't think we want to merge another functionality to swapon. The warnings are enough. For the rest we have ch{own,mod}.

Let's Keep It Simple and Stupid. We all love kisses, right? :-)

Hi Karel,

Your original suggestion for swapon to require '--force' for insecure permissions seems like the most sane thing to do - it protects the user without adding a lot of knobs. Presumably there would need to a "force" option for fstab too.

But implementing that without advance notice could lead to broken systems. Maybe it would make sense to add the --force option now and change the warning to indicate that in future versions of swapon, insecure permissions used without --force will be rejected. Then in a couple of years actually implement that change.

If you agree this is sane behavior appropriate for upstream, I'll get you a patch for swapon.

--Sarah