aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kobject_uevent.c
diff options
context:
space:
mode:
authorKay Sievers <kay.sievers@vrfy.org>2007-08-14 09:15:12 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2007-10-12 17:51:01 -0400
commit7eff2e7a8b65c25920207324e56611150eb1cd9a (patch)
tree02a0eeba9d25d996233e30c18f258dfae0ae2139 /lib/kobject_uevent.c
parent8380770c842faef3001e44662953d64ad9a93663 (diff)
Driver core: change add_uevent_var to use a struct
This changes the uevent buffer functions to use a struct instead of a long list of parameters. It does no longer require the caller to do the proper buffer termination and size accounting, which is currently wrong in some places. It fixes a known bug where parts of the uevent environment are overwritten because of wrong index calculations. Many thanks to Mathieu Desnoyers for finding bugs and improving the error handling. Signed-off-by: Kay Sievers <kay.sievers@vrfy.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> Cc: Cornelia Huck <cornelia.huck@de.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'lib/kobject_uevent.c')
-rw-r--r--lib/kobject_uevent.c149
1 files changed, 63 insertions, 86 deletions
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index e06a8dcec0f0..7d8aeb301635 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -22,8 +22,6 @@
22#include <linux/kobject.h> 22#include <linux/kobject.h>
23#include <net/sock.h> 23#include <net/sock.h>
24 24
25#define BUFFER_SIZE 2048 /* buffer for the variables */
26#define NUM_ENVP 32 /* number of env pointers */
27 25
28/* the strings here must match the enum in include/linux/kobject.h */ 26/* the strings here must match the enum in include/linux/kobject.h */
29const char *kobject_actions[] = { 27const char *kobject_actions[] = {
@@ -54,31 +52,21 @@ static struct sock *uevent_sock;
54 * corresponding error when it fails. 52 * corresponding error when it fails.
55 */ 53 */
56int kobject_uevent_env(struct kobject *kobj, enum kobject_action action, 54int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
57 char *envp_ext[]) 55 char *envp_ext[])
58{ 56{
59 char **envp; 57 struct kobj_uevent_env *env;
60 char *buffer; 58 const char *action_string = kobject_actions[action];
61 char *scratch;
62 const char *action_string;
63 const char *devpath = NULL; 59 const char *devpath = NULL;
64 const char *subsystem; 60 const char *subsystem;
65 struct kobject *top_kobj; 61 struct kobject *top_kobj;
66 struct kset *kset; 62 struct kset *kset;
67 struct kset_uevent_ops *uevent_ops; 63 struct kset_uevent_ops *uevent_ops;
68 u64 seq; 64 u64 seq;
69 char *seq_buff;
70 int i = 0; 65 int i = 0;
71 int retval = 0; 66 int retval = 0;
72 int j;
73 67
74 pr_debug("%s\n", __FUNCTION__); 68 pr_debug("%s\n", __FUNCTION__);
75 69
76 action_string = kobject_actions[action];
77 if (!action_string) {
78 pr_debug("kobject attempted to send uevent without action_string!\n");
79 return -EINVAL;
80 }
81
82 /* search the kset we belong to */ 70 /* search the kset we belong to */
83 top_kobj = kobj; 71 top_kobj = kobj;
84 while (!top_kobj->kset && top_kobj->parent) { 72 while (!top_kobj->kset && top_kobj->parent) {
@@ -92,7 +80,7 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
92 kset = top_kobj->kset; 80 kset = top_kobj->kset;
93 uevent_ops = kset->uevent_ops; 81 uevent_ops = kset->uevent_ops;
94 82
95 /* skip the event, if the filter returns zero. */ 83 /* skip the event, if the filter returns zero. */
96 if (uevent_ops && uevent_ops->filter) 84 if (uevent_ops && uevent_ops->filter)
97 if (!uevent_ops->filter(kset, kobj)) { 85 if (!uevent_ops->filter(kset, kobj)) {
98 pr_debug("kobject filter function caused the event to drop!\n"); 86 pr_debug("kobject filter function caused the event to drop!\n");
@@ -109,18 +97,11 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
109 return 0; 97 return 0;
110 } 98 }
111 99
112 /* environment index */ 100 /* environment buffer */
113 envp = kzalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL); 101 env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
114 if (!envp) 102 if (!env)
115 return -ENOMEM; 103 return -ENOMEM;
116 104
117 /* environment values */
118 buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
119 if (!buffer) {
120 retval = -ENOMEM;
121 goto exit;
122 }
123
124 /* complete object path */ 105 /* complete object path */
125 devpath = kobject_get_path(kobj, GFP_KERNEL); 106 devpath = kobject_get_path(kobj, GFP_KERNEL);
126 if (!devpath) { 107 if (!devpath) {
@@ -128,29 +109,29 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
128 goto exit; 109 goto exit;
129 } 110 }
130 111
131 /* event environemnt for helper process only */
132 envp[i++] = "HOME=/";
133 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
134
135 /* default keys */ 112 /* default keys */
136 scratch = buffer; 113 retval = add_uevent_var(env, "ACTION=%s", action_string);
137 envp [i++] = scratch; 114 if (retval)
138 scratch += sprintf(scratch, "ACTION=%s", action_string) + 1; 115 goto exit;
139 envp [i++] = scratch; 116 retval = add_uevent_var(env, "DEVPATH=%s", devpath);
140 scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1; 117 if (retval)
141 envp [i++] = scratch; 118 goto exit;
142 scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1; 119 retval = add_uevent_var(env, "SUBSYSTEM=%s", subsystem);
143 for (j = 0; envp_ext && envp_ext[j]; j++) 120 if (retval)
144 envp[i++] = envp_ext[j]; 121 goto exit;
145 /* just reserve the space, overwrite it after kset call has returned */ 122
146 envp[i++] = seq_buff = scratch; 123 /* keys passed in from the caller */
147 scratch += strlen("SEQNUM=18446744073709551616") + 1; 124 if (envp_ext) {
125 for (i = 0; envp_ext[i]; i++) {
126 retval = add_uevent_var(env, envp_ext[i]);
127 if (retval)
128 goto exit;
129 }
130 }
148 131
149 /* let the kset specific function add its stuff */ 132 /* let the kset specific function add its stuff */
150 if (uevent_ops && uevent_ops->uevent) { 133 if (uevent_ops && uevent_ops->uevent) {
151 retval = uevent_ops->uevent(kset, kobj, 134 retval = uevent_ops->uevent(kset, kobj, env);
152 &envp[i], NUM_ENVP - i, scratch,
153 BUFFER_SIZE - (scratch - buffer));
154 if (retval) { 135 if (retval) {
155 pr_debug ("%s - uevent() returned %d\n", 136 pr_debug ("%s - uevent() returned %d\n",
156 __FUNCTION__, retval); 137 __FUNCTION__, retval);
@@ -158,11 +139,13 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
158 } 139 }
159 } 140 }
160 141
161 /* we will send an event, request a new sequence number */ 142 /* we will send an event, so request a new sequence number */
162 spin_lock(&sequence_lock); 143 spin_lock(&sequence_lock);
163 seq = ++uevent_seqnum; 144 seq = ++uevent_seqnum;
164 spin_unlock(&sequence_lock); 145 spin_unlock(&sequence_lock);
165 sprintf(seq_buff, "SEQNUM=%llu", (unsigned long long)seq); 146 retval = add_uevent_var(env, "SEQNUM=%llu", (unsigned long long)seq);
147 if (retval)
148 goto exit;
166 149
167#if defined(CONFIG_NET) 150#if defined(CONFIG_NET)
168 /* send netlink message */ 151 /* send netlink message */
@@ -172,17 +155,19 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
172 155
173 /* allocate message with the maximum possible size */ 156 /* allocate message with the maximum possible size */
174 len = strlen(action_string) + strlen(devpath) + 2; 157 len = strlen(action_string) + strlen(devpath) + 2;
175 skb = alloc_skb(len + BUFFER_SIZE, GFP_KERNEL); 158 skb = alloc_skb(len + env->buflen, GFP_KERNEL);
176 if (skb) { 159 if (skb) {
160 char *scratch;
161
177 /* add header */ 162 /* add header */
178 scratch = skb_put(skb, len); 163 scratch = skb_put(skb, len);
179 sprintf(scratch, "%s@%s", action_string, devpath); 164 sprintf(scratch, "%s@%s", action_string, devpath);
180 165
181 /* copy keys to our continuous event payload buffer */ 166 /* copy keys to our continuous event payload buffer */
182 for (i = 2; envp[i]; i++) { 167 for (i = 0; i < env->envp_idx; i++) {
183 len = strlen(envp[i]) + 1; 168 len = strlen(env->envp[i]) + 1;
184 scratch = skb_put(skb, len); 169 scratch = skb_put(skb, len);
185 strcpy(scratch, envp[i]); 170 strcpy(scratch, env->envp[i]);
186 } 171 }
187 172
188 NETLINK_CB(skb).dst_group = 1; 173 NETLINK_CB(skb).dst_group = 1;
@@ -198,13 +183,19 @@ int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
198 argv [0] = uevent_helper; 183 argv [0] = uevent_helper;
199 argv [1] = (char *)subsystem; 184 argv [1] = (char *)subsystem;
200 argv [2] = NULL; 185 argv [2] = NULL;
201 call_usermodehelper (argv[0], argv, envp, UMH_WAIT_EXEC); 186 retval = add_uevent_var(env, "HOME=/");
187 if (retval)
188 goto exit;
189 retval = add_uevent_var(env, "PATH=/sbin:/bin:/usr/sbin:/usr/bin");
190 if (retval)
191 goto exit;
192
193 call_usermodehelper (argv[0], argv, env->envp, UMH_WAIT_EXEC);
202 } 194 }
203 195
204exit: 196exit:
205 kfree(devpath); 197 kfree(devpath);
206 kfree(buffer); 198 kfree(env);
207 kfree(envp);
208 return retval; 199 return retval;
209} 200}
210 201
@@ -227,52 +218,38 @@ int kobject_uevent(struct kobject *kobj, enum kobject_action action)
227EXPORT_SYMBOL_GPL(kobject_uevent); 218EXPORT_SYMBOL_GPL(kobject_uevent);
228 219
229/** 220/**
230 * add_uevent_var - helper for creating event variables 221 * add_uevent_var - add key value string to the environment buffer
231 * @envp: Pointer to table of environment variables, as passed into 222 * @env: environment buffer structure
232 * uevent() method. 223 * @format: printf format for the key=value pair
233 * @num_envp: Number of environment variable slots available, as
234 * passed into uevent() method.
235 * @cur_index: Pointer to current index into @envp. It should be
236 * initialized to 0 before the first call to add_uevent_var(),
237 * and will be incremented on success.
238 * @buffer: Pointer to buffer for environment variables, as passed
239 * into uevent() method.
240 * @buffer_size: Length of @buffer, as passed into uevent() method.
241 * @cur_len: Pointer to current length of space used in @buffer.
242 * Should be initialized to 0 before the first call to
243 * add_uevent_var(), and will be incremented on success.
244 * @format: Format for creating environment variable (of the form
245 * "XXX=%x") for snprintf().
246 * 224 *
247 * Returns 0 if environment variable was added successfully or -ENOMEM 225 * Returns 0 if environment variable was added successfully or -ENOMEM
248 * if no space was available. 226 * if no space was available.
249 */ 227 */
250int add_uevent_var(char **envp, int num_envp, int *cur_index, 228int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
251 char *buffer, int buffer_size, int *cur_len,
252 const char *format, ...)
253{ 229{
254 va_list args; 230 va_list args;
231 int len;
255 232
256 /* 233 if (env->envp_idx >= ARRAY_SIZE(env->envp)) {
257 * We check against num_envp - 1 to make sure there is at 234 printk(KERN_ERR "add_uevent_var: too many keys\n");
258 * least one slot left after we return, since kobject_uevent() 235 WARN_ON(1);
259 * needs to set the last slot to NULL.
260 */
261 if (*cur_index >= num_envp - 1)
262 return -ENOMEM; 236 return -ENOMEM;
263 237 }
264 envp[*cur_index] = buffer + *cur_len;
265 238
266 va_start(args, format); 239 va_start(args, format);
267 *cur_len += vsnprintf(envp[*cur_index], 240 len = vsnprintf(&env->buf[env->buflen],
268 max(buffer_size - *cur_len, 0), 241 sizeof(env->buf) - env->buflen,
269 format, args) + 1; 242 format, args);
270 va_end(args); 243 va_end(args);
271 244
272 if (*cur_len > buffer_size) 245 if (len >= (sizeof(env->buf) - env->buflen)) {
246 printk(KERN_ERR "add_uevent_var: buffer size too small\n");
247 WARN_ON(1);
273 return -ENOMEM; 248 return -ENOMEM;
249 }
274 250
275 (*cur_index)++; 251 env->envp[env->envp_idx++] = &env->buf[env->buflen];
252 env->buflen += len + 1;
276 return 0; 253 return 0;
277} 254}
278EXPORT_SYMBOL_GPL(add_uevent_var); 255EXPORT_SYMBOL_GPL(add_uevent_var);