Skip to content

Commit 3eef34a

Browse files
htejungregkh
authored andcommitted
kernfs: implement kernfs_get_parent(), kernfs_name/path() and friends
kernfs_node->parent and ->name are currently marked as "published" indicating that kernfs users may access them directly; however, those fields may get updated by kernfs_rename[_ns]() and unrestricted access may lead to erroneous values or oops. Protect ->parent and ->name updates with a irq-safe spinlock kernfs_rename_lock and implement the following accessors for these fields. * kernfs_name() - format the node's name into the specified buffer * kernfs_path() - format the node's path into the specified buffer * pr_cont_kernfs_name() - pr_cont a node's name (doesn't need buffer) * pr_cont_kernfs_path() - pr_cont a node's path (doesn't need buffer) * kernfs_get_parent() - pin and return a node's parent All can be called under any context. The recursive sysfs_pathname() in fs/sysfs/dir.c is replaced with kernfs_path() and sysfs_rename_dir_ns() is updated to use kernfs_get_parent() instead of dereferencing parent directly. v2: Dummy definition of kernfs_path() for !CONFIG_KERNFS was missing static inline making it cause a lot of build warnings. Add it. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 0c23b22 commit 3eef34a

3 files changed

Lines changed: 203 additions & 42 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 165 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "kernfs-internal.h"
2020

2121
DEFINE_MUTEX(kernfs_mutex);
22+
static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */
23+
static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */
2224

2325
#define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
2426

@@ -37,6 +39,141 @@ static bool kernfs_lockdep(struct kernfs_node *kn)
3739
#endif
3840
}
3941

42+
static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
43+
{
44+
return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
45+
}
46+
47+
static char * __must_check kernfs_path_locked(struct kernfs_node *kn, char *buf,
48+
size_t buflen)
49+
{
50+
char *p = buf + buflen;
51+
int len;
52+
53+
*--p = '\0';
54+
55+
do {
56+
len = strlen(kn->name);
57+
if (p - buf < len + 1) {
58+
buf[0] = '\0';
59+
p = NULL;
60+
break;
61+
}
62+
p -= len;
63+
memcpy(p, kn->name, len);
64+
*--p = '/';
65+
kn = kn->parent;
66+
} while (kn && kn->parent);
67+
68+
return p;
69+
}
70+
71+
/**
72+
* kernfs_name - obtain the name of a given node
73+
* @kn: kernfs_node of interest
74+
* @buf: buffer to copy @kn's name into
75+
* @buflen: size of @buf
76+
*
77+
* Copies the name of @kn into @buf of @buflen bytes. The behavior is
78+
* similar to strlcpy(). It returns the length of @kn's name and if @buf
79+
* isn't long enough, it's filled upto @buflen-1 and nul terminated.
80+
*
81+
* This function can be called from any context.
82+
*/
83+
int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
84+
{
85+
unsigned long flags;
86+
int ret;
87+
88+
spin_lock_irqsave(&kernfs_rename_lock, flags);
89+
ret = kernfs_name_locked(kn, buf, buflen);
90+
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
91+
return ret;
92+
}
93+
94+
/**
95+
* kernfs_path - build full path of a given node
96+
* @kn: kernfs_node of interest
97+
* @buf: buffer to copy @kn's name into
98+
* @buflen: size of @buf
99+
*
100+
* Builds and returns the full path of @kn in @buf of @buflen bytes. The
101+
* path is built from the end of @buf so the returned pointer usually
102+
* doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
103+
* and %NULL is returned.
104+
*/
105+
char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
106+
{
107+
unsigned long flags;
108+
char *p;
109+
110+
spin_lock_irqsave(&kernfs_rename_lock, flags);
111+
p = kernfs_path_locked(kn, buf, buflen);
112+
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
113+
return p;
114+
}
115+
116+
/**
117+
* pr_cont_kernfs_name - pr_cont name of a kernfs_node
118+
* @kn: kernfs_node of interest
119+
*
120+
* This function can be called from any context.
121+
*/
122+
void pr_cont_kernfs_name(struct kernfs_node *kn)
123+
{
124+
unsigned long flags;
125+
126+
spin_lock_irqsave(&kernfs_rename_lock, flags);
127+
128+
kernfs_name_locked(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
129+
pr_cont("%s", kernfs_pr_cont_buf);
130+
131+
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
132+
}
133+
134+
/**
135+
* pr_cont_kernfs_path - pr_cont path of a kernfs_node
136+
* @kn: kernfs_node of interest
137+
*
138+
* This function can be called from any context.
139+
*/
140+
void pr_cont_kernfs_path(struct kernfs_node *kn)
141+
{
142+
unsigned long flags;
143+
char *p;
144+
145+
spin_lock_irqsave(&kernfs_rename_lock, flags);
146+
147+
p = kernfs_path_locked(kn, kernfs_pr_cont_buf,
148+
sizeof(kernfs_pr_cont_buf));
149+
if (p)
150+
pr_cont("%s", p);
151+
else
152+
pr_cont("<name too long>");
153+
154+
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
155+
}
156+
157+
/**
158+
* kernfs_get_parent - determine the parent node and pin it
159+
* @kn: kernfs_node of interest
160+
*
161+
* Determines @kn's parent, pins and returns it. This function can be
162+
* called from any context.
163+
*/
164+
struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
165+
{
166+
struct kernfs_node *parent;
167+
unsigned long flags;
168+
169+
spin_lock_irqsave(&kernfs_rename_lock, flags);
170+
parent = kn->parent;
171+
kernfs_get(parent);
172+
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
173+
174+
return parent;
175+
}
176+
40177
/**
41178
* kernfs_name_hash
42179
* @name: Null terminated string to hash
@@ -1103,8 +1240,14 @@ int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
11031240
int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
11041241
const char *new_name, const void *new_ns)
11051242
{
1243+
struct kernfs_node *old_parent;
1244+
const char *old_name = NULL;
11061245
int error;
11071246

1247+
/* can't move or rename root */
1248+
if (!kn->parent)
1249+
return -EINVAL;
1250+
11081251
mutex_lock(&kernfs_mutex);
11091252

11101253
error = -ENOENT;
@@ -1126,26 +1269,38 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
11261269
new_name = kstrdup(new_name, GFP_KERNEL);
11271270
if (!new_name)
11281271
goto out;
1129-
1130-
if (kn->flags & KERNFS_STATIC_NAME)
1131-
kn->flags &= ~KERNFS_STATIC_NAME;
1132-
else
1133-
kfree(kn->name);
1134-
1135-
kn->name = new_name;
1272+
} else {
1273+
new_name = NULL;
11361274
}
11371275

11381276
/*
11391277
* Move to the appropriate place in the appropriate directories rbtree.
11401278
*/
11411279
kernfs_unlink_sibling(kn);
11421280
kernfs_get(new_parent);
1143-
kernfs_put(kn->parent);
1144-
kn->ns = new_ns;
1145-
kn->hash = kernfs_name_hash(kn->name, kn->ns);
1281+
1282+
/* rename_lock protects ->parent and ->name accessors */
1283+
spin_lock_irq(&kernfs_rename_lock);
1284+
1285+
old_parent = kn->parent;
11461286
kn->parent = new_parent;
1287+
1288+
kn->ns = new_ns;
1289+
if (new_name) {
1290+
if (!(kn->flags & KERNFS_STATIC_NAME))
1291+
old_name = kn->name;
1292+
kn->flags &= ~KERNFS_STATIC_NAME;
1293+
kn->name = new_name;
1294+
}
1295+
1296+
spin_unlock_irq(&kernfs_rename_lock);
1297+
1298+
kn->hash = kernfs_name_hash(new_name, new_ns);
11471299
kernfs_link_sibling(kn);
11481300

1301+
kernfs_put(old_parent);
1302+
kfree(old_name);
1303+
11491304
error = 0;
11501305
out:
11511306
mutex_unlock(&kernfs_mutex);

‎fs/sysfs/dir.c‎

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,18 @@
1919

2020
DEFINE_SPINLOCK(sysfs_symlink_target_lock);
2121

22-
/**
23-
* sysfs_pathname - return full path to sysfs dirent
24-
* @kn: kernfs_node whose path we want
25-
* @path: caller allocated buffer of size PATH_MAX
26-
*
27-
* Gives the name "/" to the sysfs_root entry; any path returned
28-
* is relative to wherever sysfs is mounted.
29-
*/
30-
static char *sysfs_pathname(struct kernfs_node *kn, char *path)
31-
{
32-
if (kn->parent) {
33-
sysfs_pathname(kn->parent, path);
34-
strlcat(path, "/", PATH_MAX);
35-
}
36-
strlcat(path, kn->name, PATH_MAX);
37-
return path;
38-
}
39-
4022
void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
4123
{
42-
char *path;
24+
char *buf, *path = NULL;
4325

44-
path = kzalloc(PATH_MAX, GFP_KERNEL);
45-
if (path) {
46-
sysfs_pathname(parent, path);
47-
strlcat(path, "/", PATH_MAX);
48-
strlcat(path, name, PATH_MAX);
49-
}
26+
buf = kzalloc(PATH_MAX, GFP_KERNEL);
27+
if (buf)
28+
path = kernfs_path(parent, buf, PATH_MAX);
5029

51-
WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s'\n",
52-
path ? path : name);
30+
WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s/%s'\n",
31+
path, name);
5332

54-
kfree(path);
33+
kfree(buf);
5534
}
5635

5736
/**
@@ -122,9 +101,13 @@ void sysfs_remove_dir(struct kobject *kobj)
122101
int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name,
123102
const void *new_ns)
124103
{
125-
struct kernfs_node *parent = kobj->sd->parent;
104+
struct kernfs_node *parent;
105+
int ret;
126106

127-
return kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
107+
parent = kernfs_get_parent(kobj->sd);
108+
ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns);
109+
kernfs_put(parent);
110+
return ret;
128111
}
129112

130113
int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
@@ -133,7 +116,6 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
133116
struct kernfs_node *kn = kobj->sd;
134117
struct kernfs_node *new_parent;
135118

136-
BUG_ON(!kn->parent);
137119
new_parent = new_parent_kobj && new_parent_kobj->sd ?
138120
new_parent_kobj->sd : sysfs_root_kn;
139121

‎include/linux/kernfs.h‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ struct kernfs_node {
9191
#ifdef CONFIG_DEBUG_LOCK_ALLOC
9292
struct lockdep_map dep_map;
9393
#endif
94-
/* the following two fields are published */
94+
/*
95+
* Use kernfs_get_parent() and kernfs_name/path() instead of
96+
* accessing the following two fields directly. If the node is
97+
* never moved to a different parent, it is safe to access the
98+
* parent directly.
99+
*/
95100
struct kernfs_node *parent;
96101
const char *name;
97102

@@ -229,6 +234,12 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
229234
return kn->flags & KERNFS_NS;
230235
}
231236

237+
int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
238+
char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
239+
size_t buflen);
240+
void pr_cont_kernfs_name(struct kernfs_node *kn);
241+
void pr_cont_kernfs_path(struct kernfs_node *kn);
242+
struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
232243
struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
233244
const char *name, const void *ns);
234245
void kernfs_get(struct kernfs_node *kn);
@@ -283,6 +294,19 @@ static inline void kernfs_enable_ns(struct kernfs_node *kn) { }
283294
static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
284295
{ return false; }
285296

297+
static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
298+
{ return -ENOSYS; }
299+
300+
static inline char * __must_check kernfs_path(struct kernfs_node *kn, char *buf,
301+
size_t buflen)
302+
{ return NULL; }
303+
304+
static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
305+
static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
306+
307+
static inline struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
308+
{ return NULL; }
309+
286310
static inline struct kernfs_node *
287311
kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
288312
const void *ns)

0 commit comments

Comments
 (0)