Skip to contentSkip to conversation

binderfs: free minor on binder-control creation failure

PATCH Superseded Chao Huang 4 participants 11 messages Aug 6 – Nov 30 Android drivers

lore
Share & tools
lore

Chao Huang

author
#0 lore raw

PATCH 1/1 binderfs: free minor on binder-control creation failure

From: Chao Huang <[email protected]>

binderfs_binder_ctl_create() allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Initialize minor to an invalid value and release it from the common error path after a successful allocation.

---
 drivers/android/binderfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/android/binderfs.c +6 -1
index 361d69f756f5..fdbf281d3418 100644
@@ -384,7 +384,7 @@ static const struct file_operations binder_ctl_fops = {
  */
 static int binderfs_binder_ctl_create(struct super_block *sb)
 {
-	int minor, ret;
+	int minor = -ENOSPC, ret;
 	struct dentry *dentry;
 	struct binder_device *device;
 	struct inode *inode = NULL;
@@ -441,6 +441,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 	return 0;
 
 out:
+	if (minor >= 0) {
+		mutex_lock(&binderfs_minors_mutex);
+		ida_free(&binderfs_minors, minor);
+		mutex_unlock(&binderfs_minors_mutex);
+	}
 	kfree(device);
 	iput(inode);
 

base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86

Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · Chao Huang

From: Chao Huang <[email protected]>

binderfs_binder_ctl_create() allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Quoted 4 lines

Initialize minor to an invalid value and release it from the common error path after a successful allocation.

Thanks!
Please fix drivers/android/binder/rust_binderfs.c as well.

Alice

author
#2 lore raw

PATCH v2 1/1 binderfs: free minor on binder-control creation failure

From: Chao Huang <[email protected]>

Both binderfs_binder_ctl_create() implementations allocate a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Initialize minor to an invalid value and release it from the common error path after a successful allocation in both implementations.

---
Changes in v2:
- Apply the same fix to drivers/android/binder/rust_binderfs.c.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 drivers/android/binderfs.c             | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/android/binder/rust_binderfs.c +6 -1
index ade1c4d92499..c65a8e514986 100644
@@ -375,7 +375,7 @@ static const struct file_operations binder_ctl_fops = {
  */
 static int binderfs_binder_ctl_create(struct super_block *sb)
 {
-	int minor, ret;
+	int minor = -ENOSPC, ret;
 	struct dentry *dentry;
 	struct binder_device *device;
 	struct inode *inode = NULL;
@@ -431,6 +431,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 	return 0;
 
 out:
+	if (minor >= 0) {
+		mutex_lock(&binderfs_minors_mutex);
+		ida_free(&binderfs_minors, minor);
+		mutex_unlock(&binderfs_minors_mutex);
+	}
 	kfree(device);
 	iput(inode);
 
drivers/android/binderfs.c +6 -1
index 361d69f756f5..fdbf281d3418 100644
@@ -384,7 +384,7 @@ static const struct file_operations binder_ctl_fops = {
  */
 static int binderfs_binder_ctl_create(struct super_block *sb)
 {
-	int minor, ret;
+	int minor = -ENOSPC, ret;
 	struct dentry *dentry;
 	struct binder_device *device;
 	struct inode *inode = NULL;
@@ -441,6 +441,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 	return 0;
 
 out:
+	if (minor >= 0) {
+		mutex_lock(&binderfs_minors_mutex);
+		ida_free(&binderfs_minors, minor);
+		mutex_unlock(&binderfs_minors_mutex);
+	}
 	kfree(device);
 	iput(inode);
 

base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86

Signature · 1 line
2.25.1
maintainer
#3 lore raw
Quoted 50 lines · Chao Huang

From: Chao Huang <[email protected]>

Both binderfs_binder_ctl_create() implementations allocate a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Initialize minor to an invalid value and release it from the common error path after a successful allocation in both implementations.

---
Changes in v2:
- Apply the same fix to drivers/android/binder/rust_binderfs.c.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 drivers/android/binderfs.c             | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/android/binder/rust_binderfs.c +6 -1
index ade1c4d92499..c65a8e514986 100644
@@ -375,7 +375,7 @@ static const struct file_operations binder_ctl_fops = {
  */
 static int binderfs_binder_ctl_create(struct super_block *sb)
 {
-	int minor, ret;
+	int minor = -ENOSPC, ret;
 	struct dentry *dentry;
 	struct binder_device *device;
 	struct inode *inode = NULL;
@@ -431,6 +431,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 	return 0;
 
 out:
+	if (minor >= 0) {
+		mutex_lock(&binderfs_minors_mutex);
+		ida_free(&binderfs_minors, minor);
+		mutex_unlock(&binderfs_minors_mutex);
+	}
 	kfree(device);
 	iput(inode);
 
drivers/android/binderfs.c +0 -0
index 361d69f756f5..fdbf281d3418 100644

drivers/android/binderfs.c static const struct file_operations binder_ctl_fops = { · line 387

Quoted 5 lines
drivers/android/binderfs.c +1 -1
@@ -384,7 +384,7 @@ static const struct file_operations binder_ctl_fops = {
  */
 static int binderfs_binder_ctl_create(struct super_block *sb)
 {
-	int minor, ret;
+	int minor = -ENOSPC, ret;

Why -ENOSPC?

drivers/android/binderfs.c static int binderfs_binder_ctl_create(struct super_block *sb) · line 448

Quoted 12 lines
drivers/android/binderfs.c +5 -0
 	struct dentry *dentry;
 	struct binder_device *device;
 	struct inode *inode = NULL;
@@ -441,6 +441,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 	return 0;
 
 out:
+	if (minor >= 0) {
+		mutex_lock(&binderfs_minors_mutex);
+		ida_free(&binderfs_minors, minor);
+		mutex_unlock(&binderfs_minors_mutex);
+	}

How about a new "goto out_with_minor;" tag? This removes the odd ENOSPC value and having to do this if (minor) check.

Quoted 8 lines
drivers/android/binderfs.c +0 -0
 	kfree(device);
 	iput(inode);
 

base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86

Signature · 1 line
2.25.1
author
#4 lore raw

PATCH v3 1/1 binderfs: free minor on binder-control creation failure

From: Chao Huang <[email protected]>

Both binderfs_binder_ctl_create() implementations allocate a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Release the minor from a dedicated error path after a successful allocation in both implementations.

---
Changes in v3:
- Use a dedicated out_with_minor error path instead of an -ENOSPC sentinel.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 drivers/android/binderfs.c             | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/android/binder/rust_binderfs.c +6 -1
index ade1c4d92499..0763e3db54bd 100644
@@ -421,7 +421,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -430,6 +430,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);
drivers/android/binderfs.c +6 -1
index 361d69f756f5..0e96146ecf69 100644
@@ -431,7 +431,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -440,6 +440,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);

base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86

Signature · 1 line
2.25.1
maintainer Acked-by on v3 1/1
#5 lore raw
Quoted 71 lines · Chao Huang

From: Chao Huang <[email protected]>

Both binderfs_binder_ctl_create() implementations allocate a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Release the minor from a dedicated error path after a successful allocation in both implementations.

---
Changes in v3:
- Use a dedicated out_with_minor error path instead of an -ENOSPC sentinel.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 drivers/android/binderfs.c             | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)
drivers/android/binder/rust_binderfs.c +6 -1
index ade1c4d92499..0763e3db54bd 100644
@@ -421,7 +421,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -430,6 +430,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);
drivers/android/binderfs.c +6 -1
index 361d69f756f5..0e96146ecf69 100644
@@ -431,7 +431,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -440,6 +440,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);
Quoted 4 lines

base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86

Signature · 1 line
2.25.1

LGTM,

author
#6 lore raw

PATCH v4 1/1 binderfs: free minor on binder-control creation failure

In reply to Chao Huang

From: Chao Huang <[email protected]>

The binderfs_binder_ctl_create() implementation in rust_binderfs.c allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Release the minor from a dedicated error path after a successful allocation.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

---
Changes in v4:
- Update only rust_binderfs.c because drivers/android/binderfs.c has been removed from the current tree.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/android/binder/rust_binderfs.c +6 -1
index 5f020226720d..47c4ca9ea74d 100644
@@ -427,7 +427,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -436,6 +436,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#7 lore raw
Quoted 11 lines · Chao Huang

From: Chao Huang <[email protected]>

The binderfs_binder_ctl_create() implementation in rust_binderfs.c allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Release the minor from a dedicated error path after a successful allocation.

Quoted 7 lines
Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

---
Changes in v4:
- Update only rust_binderfs.c because drivers/android/binderfs.c has been removed from the current tree.

What about versions 1-3?

And you didn't sign off on this :(

author
#8 lore raw

PATCH v5 1/1 rust_binder: free minor on binder-control creation failure

From: Chao Huang <[email protected]>

The binderfs_binder_ctl_create() implementation in rust_binderfs.c allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Release the minor from a dedicated error path after a successful allocation.

Fixes eafedbc7c050 ("rust_binder: add Rust Binder driver")

---
v4 -> v5:
- Change the subject prefix to rust_binder and include the complete revision history.

v3 -> v4:
- Update only rust_binderfs.c because drivers/android/binderfs.c has been removed from the current tree.

v2 -> v3:
- Use a dedicated out_with_minor error path instead of an -ENOSPC sentinel.

v1 -> v2:
- Apply the same fix to drivers/android/binder/rust_binderfs.c.

 drivers/android/binder/rust_binderfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/android/binder/rust_binderfs.c +6 -1
index 5f020226720d..47c4ca9ea74d 100644
@@ -427,7 +427,7 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	dentry = d_alloc_name(root, "binder-control");
 	if (!dentry)
-		goto out;
+		goto out_with_minor;
 
 	inode->i_private = device;
 	info->control_dentry = dentry;
@@ -436,6 +436,11 @@ static int binderfs_binder_ctl_create(struct super_block *sb)
 
 	return 0;
 
+out_with_minor:
+	mutex_lock(&binderfs_minors_mutex);
+	ida_free(&binderfs_minors, minor);
+	mutex_unlock(&binderfs_minors_mutex);
+
 out:
 	kfree(device);
 	iput(inode);
Signature · 1 line
2.54.0 (Apple Git-157)
maintainer
#9 lore raw

Re: [PATCH v5] rust_binder: free minor on binder-control creation failure

Quoted 5 lines · Chao Huang

The binderfs_binder_ctl_create() implementation in rust_binderfs.c allocates a minor before creating the binder-control dentry. If d_alloc_name() fails, the error path frees the device and drops the inode, but leaves the minor allocated in binderfs_minors. Repeated failures can therefore exhaust the global minor IDA.

Thanks for the v5, though this fix has already been picked up into char-misc-next via commit 16ecdcab15fb ("rust_binderfs: fix minor number leak on binder-control creation failure") [1].

[1] https://lore.kernel.org/r/[email protected]

Alice

author
#10 lore raw

Re: [PATCH v5] rust_binder: free minor on binder-control creation failure

From: Chao Huang <[email protected]>

Quoted 1 line · Alice Ryhl

Thanks for the v5, though this fix has already been picked up...

Thanks for letting me know. I'll drop this patch and won't resend it.

Chao