Skip to contentSkip to conversation

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

PATCH In review Mike Rapoport (Microsoft) 3 participants 5 messages Jun 30 – Jul 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

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.

---
This is a (tiny) part of larger work of replacing page allocator calls with kmalloc:

Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/security
---
v4 changes:
* rebase on v7.2-rc1

v3: https://lore.kernel.org/all/[email protected]
* get the args in the right order

v2: https://lore.kernel.org/all/[email protected]
* 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 1a713d96206f..d1f089917a82 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(PATH_MAX, GFP_KERNEL);
 	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;
 }
 

---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260520-security-6cdd60da7129

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
#1 lore raw

Should have been:

Subject: [PATCH v4] selinux: hooks: use kmalloc() to allocate path buffer

Quoted 67 lines · Mike Rapoport (Microsoft)

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.

---
This is a (tiny) part of larger work of replacing page allocator calls with kmalloc:

Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/security
---
v4 changes:
* rebase on v7.2-rc1

v3: https://lore.kernel.org/all/[email protected]
* get the args in the right order

v2: https://lore.kernel.org/all/[email protected]
* explicitly use kmalloc() with PATH_MAX

---
 security/selinux/hooks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +3 -4
index 1a713d96206f..d1f089917a82 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(PATH_MAX, GFP_KERNEL);
 	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;
 }
 
 
---

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260520-security-6cdd60da7129

Best regards,

Signature · 2 lines
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#2 lore raw
Quoted 4 lines · Mike Rapoport

Should have been:

Subject: [PATCH v4] selinux: hooks: use kmalloc() to allocate path buffer

No worries, I just merged the v3 patchset, it applied cleanly.

Quoted 72 lines · Mike Rapoport (Microsoft)
Quoted 62 lines

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.

---
This is a (tiny) part of larger work of replacing page allocator calls with kmalloc:

Also in git:
https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git gfp-to-kmalloc/security
---
v4 changes:
* rebase on v7.2-rc1

v3: https://lore.kernel.org/all/[email protected]
* get the args in the right order

v2: https://lore.kernel.org/all/[email protected]
* explicitly use kmalloc() with PATH_MAX

---
 security/selinux/hooks.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
security/selinux/hooks.c +3 -4
index 1a713d96206f..d1f089917a82 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(PATH_MAX, GFP_KERNEL);
      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;
 }
 
 
---

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260520-security-6cdd60da7129

Quoted 5 lines

Best regards,
--
Sincerely yours,
Mike.

--
Sincerely yours,
Mike.

Signature · 1 line
paul-moore.com
#3 lore raw
Quoted 7 lines · Paul Moore
Quoted 4 lines · Mike Rapoport

Should have been:

Subject: [PATCH v4] selinux: hooks: use kmalloc() to allocate path buffer

No worries, I just merged the v3 patchset, it applied cleanly.

Thanks, Paul!

Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
In reply to Mike Rapoport (Microsoft)
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

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.

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1