Skip to contentSkip to conversation

Use dput() after d_find_alias().

PATCH Applied Cyril Roelandt 4 participants 7 messages Nov 11, 2012 – Nov 20, 2012

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

cover Use dput() after d_find_alias().

Hello,

A call to d_find_alias() needs a corresponding call to dput() in order to avoid resource leaks.

The first patch is a Coccinelle scripts that looks for missing calls to dput(). The other two patches add calls to dput() where needed.

WBR,
Cyril Roelandt.

#1 lore raw

PATCH 1/3 Coccinelle: Add api/d_find_alias.cocci.

Ensure that calls to d_find_alias() have a corresponding dput().

---
 scripts/coccinelle/api/d_find_alias.cocci |   80 +++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)
 create mode 100644 scripts/coccinelle/api/d_find_alias.cocci
scripts/coccinelle/api/d_find_alias.cocci new file +80 -0
new file mode 100644
index 0000000..639eec7
@@ -0,0 +1,80 @@
+/// Make sure calls to d_find_alias() have a corresponding call to dput().
+//
+// Keywords: d_find_alias, dput
+//
+// Confidence: Moderate
+// URL: http://coccinelle.lip6.fr/
+// Options: -include_headers
+
+virtual context
+virtual org
+virtual patch
+virtual report
+
+ at r exists@
+local idexpression struct dentry *dent;
+expression E, E1;
+statement S1, S2;
+position p1, p2;
+@@
+(
+	if (!(dent at p1 = d_find_alias(...))) S1
+|
+	dent at p1 = d_find_alias(...)
+)
+
+<...when != dput(dent)
+    when != if (...) { <+... dput(dent) ...+> }
+    when != true !dent || ...
+    when != dent = E
+    when != E = dent
+if (!dent || ...) S2
+...>
+(
+	return <+...dent...+>;
+|
+	return @p2 ...;
+|
+	dent at p2 = E1;
+|
+	E1 = dent;
+)
+
+ at depends on context@
+local idexpression struct dentry *r.dent;
+position r.p1,r.p2;
+@@
+* dent at p1 = ...
+  ...
+(
+* return at p2 ...;
+|
+* dent at p2
+)
+
+
+ at script:python depends on org@
+p1 << r.p1;
+p2 << r.p2;
+@@
+cocci.print_main("Missing call to dput()",p1)
+cocci.print_secs("",p2)
+
+ at depends on patch@
+local idexpression struct dentry *r.dent;
+position r.p2;
+@@
+(
++ dput(dent);
+  return @p2 ...;
+|
++ dput(dent);
+  dent at p2 = ...;
+)
+
+ at script:python depends on report@
+p1 << r.p1;
+p2 << r.p2;
+@@
+msg = "Missing call to dput()@line %s."
+coccilib.report.print_report(p1[0], msg % (p2[0].line))
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/3 iommu/tegra-smmu.c: fix dentry reference leak in smmu_debugfs_stats_show().

In reply to Cyril Roelandt

Call to d_find_alias() needs a corresponding dput().

---
 drivers/iommu/tegra-smmu.c |    1 +
 1 file changed, 1 insertion(+)
drivers/iommu/tegra-smmu.c +1 -0
index 4167863..4252d74 100644
@@ -1052,6 +1052,7 @@ static int smmu_debugfs_stats_show(struct seq_file *s, void *v)
 			stats[i], val, offs);
 	}
 	seq_printf(s, "\n");
+	dput(dent);
 
 	return 0;
 }
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH 3/3 ceph: fix dentry reference leak in ceph_encode_fh().

In reply to Cyril Roelandt

dput() was not called in the error path.

---
 fs/ceph/export.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ceph/export.c +3 -1
index 9349bb3..ca3ab3f 100644
@@ -56,13 +56,15 @@ static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
 	struct ceph_nfs_confh *cfh = (void *)rawfh;
 	int connected_handle_length = sizeof(*cfh)/4;
 	int handle_length = sizeof(*fh)/4;
-	struct dentry *dentry = d_find_alias(inode);
+	struct dentry *dentry;
 	struct dentry *parent;
 
 	/* don't re-export snaps */
 	if (ceph_snap(inode) != CEPH_NOSNAP)
 		return -EINVAL;
 
+	dentry = d_find_alias(inode);
+
 	/* if we found an alias, generate a connectable fh */
 	if (*max_len >= connected_handle_length && dentry) {
 		dout("encode_fh %p connectable\n", dentry);
Signature · 1 line
1.7.10.4
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH 3/3] ceph: fix dentry reference leak in ceph_encode_fh().

Quoted 3 lines · Cyril Roelandt

dput() was not called in the error path.

This looks good, thanks a lot. I'll apply it.

Quoted 26 lines
---
 fs/ceph/export.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/ceph/export.c +3 -1
index 9349bb3..ca3ab3f 100644
@@ -56,13 +56,15 @@ static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
 	struct ceph_nfs_confh *cfh = (void *)rawfh;
 	int connected_handle_length = sizeof(*cfh)/4;
 	int handle_length = sizeof(*fh)/4;
-	struct dentry *dentry = d_find_alias(inode);
+	struct dentry *dentry;
 	struct dentry *parent;
 
 	/* don't re-export snaps */
 	if (ceph_snap(inode) != CEPH_NOSNAP)
 		return -EINVAL;
 
+	dentry = d_find_alias(inode);
+
 	/* if we found an alias, generate a connectable fh */
 	if (*max_len >= connected_handle_length && dentry) {
 		dout("encode_fh %p connectable\n", dentry);