All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] a few devtmpfs patches
@ 2020-01-15 18:41 Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 1/5] devtmpfs: fix theoretical stale pointer deref in devtmpfsd() Rasmus Villemoes
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, linux-kernel; +Cc: Rasmus Villemoes

Patch 1 fixes a mostly theoretical problem, but as this is core code,
the pattern might be copy-pasted, so it might as well be fixed. The
remaining ones reduce memory footprint a bit by initifying a function
and factoring out common code.

Rasmus Villemoes (5):
  devtmpfs: fix theoretical stale pointer deref in devtmpfsd()
  devtmpfs: factor out setup part of devtmpfsd()
  devtmpfs: simplify initialization of mount_dev
  devtmpfs: initify a bit
  devtmpfs: factor out common tail of devtmpfs_{create,delete}_node

 drivers/base/devtmpfs.c | 79 ++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 40 deletions(-)

-- 
2.23.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] devtmpfs: fix theoretical stale pointer deref in devtmpfsd()
  2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
@ 2020-01-15 18:41 ` Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 2/5] devtmpfs: factor out setup part of devtmpfsd() Rasmus Villemoes
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Rasmus Villemoes, linux-kernel

After complete(&setup_done), devtmpfs_init proceeds and may actually
return, invalidating the *err pointer, before devtmpfsd() proceeds to
reading back *err.

This is of course completely theoretical since the error conditions
never trigger in practice, and even if they did, nobody cares about
the exit value from a kernel thread, so it doesn't matter if we happen
to read back some garbage from some other stack frame. Still, this
isn't a pattern that should be copy-pasted, so fix it.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/base/devtmpfs.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index 6cdbf1531238..ccb046fe12b7 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -390,12 +390,13 @@ static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
 
 static int devtmpfsd(void *p)
 {
-	int *err = p;
-	*err = ksys_unshare(CLONE_NEWNS);
-	if (*err)
+	int err;
+
+	err = ksys_unshare(CLONE_NEWNS);
+	if (err)
 		goto out;
-	*err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
-	if (*err)
+	err = do_mount("devtmpfs", "/", "devtmpfs", MS_SILENT, NULL);
+	if (err)
 		goto out;
 	ksys_chdir("/.."); /* will traverse into overmounted root */
 	ksys_chroot(".");
@@ -421,8 +422,9 @@ static int devtmpfsd(void *p)
 	}
 	return 0;
 out:
+	*(int *)p = err;
 	complete(&setup_done);
-	return *err;
+	return err;
 }
 
 /*
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/5] devtmpfs: factor out setup part of devtmpfsd()
  2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 1/5] devtmpfs: fix theoretical stale pointer deref in devtmpfsd() Rasmus Villemoes
@ 2020-01-15 18:41 ` Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 3/5] devtmpfs: simplify initialization of mount_dev Rasmus Villemoes
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Rasmus Villemoes, linux-kernel

Factor out the setup part of devtmpfsd() to make it a bit easier to
see that we always call setup_done() exactly once (provided of course
the kthread is succesfully created).

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/base/devtmpfs.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index ccb046fe12b7..963889331bb4 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -388,7 +388,7 @@ static int handle(const char *name, umode_t mode, kuid_t uid, kgid_t gid,
 		return handle_remove(name, dev);
 }
 
-static int devtmpfsd(void *p)
+static int devtmpfs_setup(void *p)
 {
 	int err;
 
@@ -400,7 +400,18 @@ static int devtmpfsd(void *p)
 		goto out;
 	ksys_chdir("/.."); /* will traverse into overmounted root */
 	ksys_chroot(".");
+out:
+	*(int *)p = err;
 	complete(&setup_done);
+	return err;
+}
+
+static int devtmpfsd(void *p)
+{
+	int err = devtmpfs_setup(p);
+
+	if (err)
+		return err;
 	while (1) {
 		spin_lock(&req_lock);
 		while (requests) {
@@ -421,10 +432,6 @@ static int devtmpfsd(void *p)
 		schedule();
 	}
 	return 0;
-out:
-	*(int *)p = err;
-	complete(&setup_done);
-	return err;
 }
 
 /*
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/5] devtmpfs: simplify initialization of mount_dev
  2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 1/5] devtmpfs: fix theoretical stale pointer deref in devtmpfsd() Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 2/5] devtmpfs: factor out setup part of devtmpfsd() Rasmus Villemoes
@ 2020-01-15 18:41 ` Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 4/5] devtmpfs: initify a bit Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 5/5] devtmpfs: factor out common tail of devtmpfs_{create,delete}_node Rasmus Villemoes
  4 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Rasmus Villemoes, linux-kernel

Avoid a bit of ifdeffery by using the IS_ENABLED() helper.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/base/devtmpfs.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index 963889331bb4..693390d9c545 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -30,11 +30,7 @@
 
 static struct task_struct *thread;
 
-#if defined CONFIG_DEVTMPFS_MOUNT
-static int mount_dev = 1;
-#else
-static int mount_dev;
-#endif
+static int mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
 
 static DEFINE_SPINLOCK(req_lock);
 
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/5] devtmpfs: initify a bit
  2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
                   ` (2 preceding siblings ...)
  2020-01-15 18:41 ` [PATCH 3/5] devtmpfs: simplify initialization of mount_dev Rasmus Villemoes
@ 2020-01-15 18:41 ` Rasmus Villemoes
  2020-01-15 18:41 ` [PATCH 5/5] devtmpfs: factor out common tail of devtmpfs_{create,delete}_node Rasmus Villemoes
  4 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Rasmus Villemoes, linux-kernel

devtmpfs_mount() is only called from prepare_namespace() in
init/do_mounts.c, which is an __init function, so devtmpfs_mount() can
also be moved to .init.text.

Then the mount_dev static variable is only referenced from __init
functions (devtmpfs_mount and its initializer function mount_param).

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/base/devtmpfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index 693390d9c545..56632fb22fc0 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -30,7 +30,7 @@
 
 static struct task_struct *thread;
 
-static int mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
+static int __initdata mount_dev = IS_ENABLED(CONFIG_DEVTMPFS_MOUNT);
 
 static DEFINE_SPINLOCK(req_lock);
 
@@ -355,7 +355,7 @@ static int handle_remove(const char *nodename, struct device *dev)
  * If configured, or requested by the commandline, devtmpfs will be
  * auto-mounted after the kernel mounted the root filesystem.
  */
-int devtmpfs_mount(void)
+int __init devtmpfs_mount(void)
 {
 	int err;
 
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 5/5] devtmpfs: factor out common tail of devtmpfs_{create,delete}_node
  2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
                   ` (3 preceding siblings ...)
  2020-01-15 18:41 ` [PATCH 4/5] devtmpfs: initify a bit Rasmus Villemoes
@ 2020-01-15 18:41 ` Rasmus Villemoes
  4 siblings, 0 replies; 6+ messages in thread
From: Rasmus Villemoes @ 2020-01-15 18:41 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki; +Cc: Rasmus Villemoes, linux-kernel

There's some common boilerplate in devtmpfs_{create,delete}_node, put
that in a little helper.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
 drivers/base/devtmpfs.c | 44 ++++++++++++++++++-----------------------
 1 file changed, 19 insertions(+), 25 deletions(-)

diff --git a/drivers/base/devtmpfs.c b/drivers/base/devtmpfs.c
index 56632fb22fc0..5995c437cbdf 100644
--- a/drivers/base/devtmpfs.c
+++ b/drivers/base/devtmpfs.c
@@ -89,6 +89,23 @@ static inline int is_blockdev(struct device *dev)
 static inline int is_blockdev(struct device *dev) { return 0; }
 #endif
 
+static int devtmpfs_submit_req(struct req *req, const char *tmp)
+{
+	init_completion(&req->done);
+
+	spin_lock(&req_lock);
+	req->next = requests;
+	requests = req;
+	spin_unlock(&req_lock);
+
+	wake_up_process(thread);
+	wait_for_completion(&req->done);
+
+	kfree(tmp);
+
+	return req->err;
+}
+
 int devtmpfs_create_node(struct device *dev)
 {
 	const char *tmp = NULL;
@@ -113,19 +130,7 @@ int devtmpfs_create_node(struct device *dev)
 
 	req.dev = dev;
 
-	init_completion(&req.done);
-
-	spin_lock(&req_lock);
-	req.next = requests;
-	requests = &req;
-	spin_unlock(&req_lock);
-
-	wake_up_process(thread);
-	wait_for_completion(&req.done);
-
-	kfree(tmp);
-
-	return req.err;
+	return devtmpfs_submit_req(&req, tmp);
 }
 
 int devtmpfs_delete_node(struct device *dev)
@@ -143,18 +148,7 @@ int devtmpfs_delete_node(struct device *dev)
 	req.mode = 0;
 	req.dev = dev;
 
-	init_completion(&req.done);
-
-	spin_lock(&req_lock);
-	req.next = requests;
-	requests = &req;
-	spin_unlock(&req_lock);
-
-	wake_up_process(thread);
-	wait_for_completion(&req.done);
-
-	kfree(tmp);
-	return req.err;
+	return devtmpfs_submit_req(&req, tmp);
 }
 
 static int dev_mkdir(const char *name, umode_t mode)
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-01-15 18:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-15 18:41 [PATCH 0/5] a few devtmpfs patches Rasmus Villemoes
2020-01-15 18:41 ` [PATCH 1/5] devtmpfs: fix theoretical stale pointer deref in devtmpfsd() Rasmus Villemoes
2020-01-15 18:41 ` [PATCH 2/5] devtmpfs: factor out setup part of devtmpfsd() Rasmus Villemoes
2020-01-15 18:41 ` [PATCH 3/5] devtmpfs: simplify initialization of mount_dev Rasmus Villemoes
2020-01-15 18:41 ` [PATCH 4/5] devtmpfs: initify a bit Rasmus Villemoes
2020-01-15 18:41 ` [PATCH 5/5] devtmpfs: factor out common tail of devtmpfs_{create,delete}_node Rasmus Villemoes

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.