Skip to contentSkip to conversation

selinux: hooks: use kmalloc() to allocate path buffer

PATCH Superseded Mike Rapoport 2 participants 3 messages May 31 SELinux security module

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selinux: hooks: use kmalloc() to allocate path buffer

From: "Mike Rapoport (Microsoft)" <[email protected]>

selinux_genfs_get_sid() allocates memory for a path with __get_free_page().

Such usage does not require a "page" and the size of the buffer should actually be PATH_MAX which may be less than PAGE_SIZE on some architectures.

Replace __get_free_page() for allocation of a path buffer with kmalloc() and make it explicit that the buffer size is PATH_MAX.

---
v2 changes:
* explicitly use kmalloc() with PATH_MAX

 security/selinux/hooks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +3 -3
index 0f704380a8c8..19493198ece1 100644
@@ -1336,11 +1336,11 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 	struct super_block *sb = dentry->d_sb;
 	char *buffer, *path;
 
-	buffer = (char *)__get_free_page(GFP_KERNEL);
+	buffer = kmalloc(GFP_KERNEL, PATH_MAX);
 	if (!buffer)
 		return -ENOMEM;
 
-	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
+	path = dentry_path_raw(dentry, buffer, PATH_MAX);
 	if (IS_ERR(path))
 		rc = PTR_ERR(path);
 	else {
@@ -1361,7 +1361,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 			rc = 0;
 		}
 	}
-	free_page((unsigned long)buffer);
+	kfree(buffer);
 	return rc;
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 24 lines · On Sun, 31 May 2026 18:15:02 +0300 Mike Rapoport <[email protected]> wrote:

From: "Mike Rapoport (Microsoft)" <[email protected]>

selinux_genfs_get_sid() allocates memory for a path with __get_free_page().

Such usage does not require a "page" and the size of the buffer should actually be PATH_MAX which may be less than PAGE_SIZE on some architectures.

Replace __get_free_page() for allocation of a path buffer with kmalloc() and make it explicit that the buffer size is PATH_MAX.

---
v2 changes:
* explicitly use kmalloc() with PATH_MAX

 security/selinux/hooks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +0 -0
index 0f704380a8c8..19493198ece1 100644

security/selinux/hooks.c static int selinux_genfs_get_sid(struct dentry *dentry, · line 1339

Quoted 5 lines
security/selinux/hooks.c +1 -1
@@ -1336,11 +1336,11 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 	struct super_block *sb = dentry->d_sb;
 	char *buffer, *path;
 
-	buffer = (char *)__get_free_page(GFP_KERNEL);
+	buffer = kmalloc(GFP_KERNEL, PATH_MAX);

At least get the args in the right order.

Quoted 17 lines
security/selinux/hooks.c +2 -2
 	if (!buffer)
 		return -ENOMEM;
 
-	path = dentry_path_raw(dentry, buffer, PAGE_SIZE);
+	path = dentry_path_raw(dentry, buffer, PATH_MAX);
 	if (IS_ERR(path))
 		rc = PTR_ERR(path);
 	else {
@@ -1361,7 +1361,7 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 			rc = 0;
 		}
 	}
-	free_page((unsigned long)buffer);
+	kfree(buffer);
 	return rc;
 }
 
#2 lore raw
Quoted 30 lines · David Laight
Quoted 27 lines · On Sun, 31 May 2026 18:15:02 +0300 Mike Rapoport <[email protected]> wrote:

From: "Mike Rapoport (Microsoft)" <[email protected]>

selinux_genfs_get_sid() allocates memory for a path with __get_free_page().

Such usage does not require a "page" and the size of the buffer should actually be PATH_MAX which may be less than PAGE_SIZE on some architectures.

Replace __get_free_page() for allocation of a path buffer with kmalloc() and make it explicit that the buffer size is PATH_MAX.

---
v2 changes:
* explicitly use kmalloc() with PATH_MAX

 security/selinux/hooks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +0 -0
index 0f704380a8c8..19493198ece1 100644
@@ -1336,11 +1336,11 @@ static int selinux_genfs_get_sid(struct dentry *dentry,
 	struct super_block *sb = dentry->d_sb;
 	char *buffer, *path;
 
Quoted 4 lines

security/selinux/hooks.c static int selinux_genfs_get_sid(struct dentry *dentry, · line 1339

Quoted 2 lines
security/selinux/hooks.c +1 -1
-	buffer = (char *)__get_free_page(GFP_KERNEL);
+	buffer = kmalloc(GFP_KERNEL, PATH_MAX);

At least get the args in the right order.

oops

Signature · 2 lines
Sincerely yours,
Mike.