Skip to contentSkip to conversation

vfs: use kstrdup() and check failing allocation

PATCH Applied Li Zefan 4 participants 5 messages Jul 21, 2008 VFS

lore
Share & tools
lore

Li Zefan

author
#0 lore raw

PATCH 1/1 vfs: use kstrdup() and check failing allocation

- use kstrdup() instead of kmalloc() + memcpy()
- return NULL if allocating ->mnt_devname failed
- mnt_devname should be const

---

Andrew, please drop vfs-use-kstrdup.patch

---

 fs/namespace.c        |   24 +++++++++++++-----------
 include/linux/mount.h |    2 +-
 2 files changed, 14 insertions(+), 12 deletions(-)
fs/namespace.c +13 -11
index 4f6f763..e52e11f 100644
@@ -112,9 +112,13 @@ struct vfsmount *alloc_vfsmnt(const char *name)
 		int err;
 
 		err = mnt_alloc_id(mnt);
-		if (err) {
-			kmem_cache_free(mnt_cache, mnt);
-			return NULL;
+		if (err)
+			goto out_free_cache;
+
+		if (name) {
+			mnt->mnt_devname = kstrdup(name, GFP_KERNEL);
+			if (!mnt->mnt_devname)
+				goto out_free_id;
 		}
 
 		atomic_set(&mnt->mnt_count, 1);
@@ -127,16 +131,14 @@ struct vfsmount *alloc_vfsmnt(const char *name)
 		INIT_LIST_HEAD(&mnt->mnt_slave_list);
 		INIT_LIST_HEAD(&mnt->mnt_slave);
 		atomic_set(&mnt->__mnt_writers, 0);
-		if (name) {
-			int size = strlen(name) + 1;
-			char *newname = kmalloc(size, GFP_KERNEL);
-			if (newname) {
-				memcpy(newname, name, size);
-				mnt->mnt_devname = newname;
-			}
-		}
 	}
 	return mnt;
+
+out_free_id:
+	mnt_free_id(mnt);
+out_free_cache:
+	kmem_cache_free(mnt_cache, mnt);
+	return NULL;
 }
 
 /*
include/linux/mount.h +1 -1
index 4374d1a..b5efaa2 100644
@@ -47,7 +47,7 @@ struct vfsmount {
 	struct list_head mnt_child;	/* and going through their mnt_child */
 	int mnt_flags;
 	/* 4 bytes hole on 64bits arches */
-	char *mnt_devname;		/* Name of device e.g. /dev/dsk/hda1 */
+	const char *mnt_devname;	/* Name of device e.g. /dev/dsk/hda1 */
 	struct list_head mnt_list;
 	struct list_head mnt_expire;	/* link in fs-specific expiry list */
 	struct list_head mnt_share;	/* circular list of shared mounts */
Signature · 1 line
1.5.4.rc3
Acked-by on v1 1/1
#1 lore raw

[Li Zefan - Mon, Jul 21, 2008 at 06:06:36PM +0800]

| - use kstrdup() instead of kmalloc() + memcpy()
| - return NULL if allocating ->mnt_devname failed
| - mnt_devname should be const
| 
| Signed-off-by: Li Zefan <[email protected]>
| ---
| 
|   Andrew, please drop vfs-use-kstrdup.patch
| 
| ---

Thanks Li. I think Al will be fine with this version.

So if you needed:

- Cyrill -

Reviewed-by
#2 lore raw
Quoted 12 lines · Cyrill Gorcunov

[Li Zefan - Mon, Jul 21, 2008 at 06:06:36PM +0800]

| - use kstrdup() instead of kmalloc() + memcpy()
| - return NULL if allocating ->mnt_devname failed
| - mnt_devname should be const
| 
| Signed-off-by: Li Zefan <[email protected]>
| ---
| 
|   Andrew, please drop vfs-use-kstrdup.patch
| 
| ---
Quoted 4 lines

Thanks Li. I think Al will be fine with this version.

So if you needed:

maintainer Applied
#3 lore raw
In reply to Cyrill Gorcunov
Quoted 12 lines · Cyrill Gorcunov

[Li Zefan - Mon, Jul 21, 2008 at 06:06:36PM +0800]

| - use kstrdup() instead of kmalloc() + memcpy()
| - return NULL if allocating ->mnt_devname failed
| - mnt_devname should be const
| 
| Signed-off-by: Li Zefan <[email protected]>
| ---
| 
|   Andrew, please drop vfs-use-kstrdup.patch
| 
| ---
Quoted 4 lines

Thanks Li. I think Al will be fine with this version.

So if you needed:

Applied.

#4 lore raw

[Al Viro - Mon, Jul 21, 2008 at 02:24:29PM +0100]
[...]

| > So if you needed:
| > Acked-by: Cyrill Gorcunov <[email protected]>
| 
| Applied.
| 

Thanks Al!

- Cyrill -