Skip to content

Commit e9fe690

Browse files
jankaratorvalds
authored andcommitted
inotify: provide function for name length rounding
Rounding of name length when passing it to userspace was done in several places. Provide a function to do it and use it in all places. Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Christoph Hellwig <hch@lst.de> Cc: Eric Paris <eparis@parisplace.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0abdd7a commit e9fe690

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

‎fs/notify/inotify/inotify_user.c‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ static unsigned int inotify_poll(struct file *file, poll_table *wait)
124124
return ret;
125125
}
126126

127+
static int round_event_name_len(struct fsnotify_event *event)
128+
{
129+
if (!event->name_len)
130+
return 0;
131+
return roundup(event->name_len + 1, sizeof(struct inotify_event));
132+
}
133+
127134
/*
128135
* Get an inotify_kernel_event if one exists and is small
129136
* enough to fit in "count". Return an error pointer if
@@ -144,9 +151,7 @@ static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
144151

145152
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
146153

147-
if (event->name_len)
148-
event_size += roundup(event->name_len + 1, event_size);
149-
154+
event_size += round_event_name_len(event);
150155
if (event_size > count)
151156
return ERR_PTR(-EINVAL);
152157

@@ -171,7 +176,8 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
171176
struct fsnotify_event_private_data *fsn_priv;
172177
struct inotify_event_private_data *priv;
173178
size_t event_size = sizeof(struct inotify_event);
174-
size_t name_len = 0;
179+
size_t name_len;
180+
size_t pad_name_len;
175181

176182
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
177183

@@ -189,14 +195,13 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
189195
inotify_free_event_priv(fsn_priv);
190196
}
191197

198+
name_len = event->name_len;
192199
/*
193-
* round up event->name_len so it is a multiple of event_size
200+
* round up name length so it is a multiple of event_size
194201
* plus an extra byte for the terminating '\0'.
195202
*/
196-
if (event->name_len)
197-
name_len = roundup(event->name_len + 1, event_size);
198-
inotify_event.len = name_len;
199-
203+
pad_name_len = round_event_name_len(event);
204+
inotify_event.len = pad_name_len;
200205
inotify_event.mask = inotify_mask_to_arg(event->mask);
201206
inotify_event.cookie = event->sync_cookie;
202207

@@ -209,20 +214,18 @@ static ssize_t copy_event_to_user(struct fsnotify_group *group,
209214
/*
210215
* fsnotify only stores the pathname, so here we have to send the pathname
211216
* and then pad that pathname out to a multiple of sizeof(inotify_event)
212-
* with zeros. I get my zeros from the nul_inotify_event.
217+
* with zeros.
213218
*/
214-
if (name_len) {
215-
unsigned int len_to_zero = name_len - event->name_len;
219+
if (pad_name_len) {
216220
/* copy the path name */
217-
if (copy_to_user(buf, event->file_name, event->name_len))
221+
if (copy_to_user(buf, event->file_name, name_len))
218222
return -EFAULT;
219-
buf += event->name_len;
223+
buf += name_len;
220224

221225
/* fill userspace with 0's */
222-
if (clear_user(buf, len_to_zero))
226+
if (clear_user(buf, pad_name_len - name_len))
223227
return -EFAULT;
224-
buf += len_to_zero;
225-
event_size += name_len;
228+
event_size += pad_name_len;
226229
}
227230

228231
return event_size;
@@ -314,9 +317,7 @@ static long inotify_ioctl(struct file *file, unsigned int cmd,
314317
list_for_each_entry(holder, &group->notification_list, event_list) {
315318
event = holder->event;
316319
send_len += sizeof(struct inotify_event);
317-
if (event->name_len)
318-
send_len += roundup(event->name_len + 1,
319-
sizeof(struct inotify_event));
320+
send_len += round_event_name_len(event);
320321
}
321322
mutex_unlock(&group->notification_mutex);
322323
ret = put_user(send_len, (int __user *) p);

0 commit comments

Comments
 (0)