Skip to content

Commit

Permalink
sysctl: Create local copies of directory names used in paths
Browse files Browse the repository at this point in the history
Creating local copies of directory names is a good idea for
two reasons.
- The dynamic names used by callers must be copied into new
  strings by the callers today to ensure the strings do not
  change between register and unregister of the sysctl table.

- Sysctl directories have a potentially different lifetime
  than the time between register and unregister of any
  particular sysctl table.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
  • Loading branch information
Eric W. Biederman committed Jan 25, 2012
1 parent bd295b5 commit f05e53a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions fs/proc/proc_sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,12 @@ struct ctl_table_header *__register_sysctl_paths(
struct ctl_table *new, **prevp;
unsigned int n, npath;
struct ctl_table_set *set;
size_t path_bytes = 0;
char *new_name;

/* Count the path components */
for (npath = 0; path[npath].procname; ++npath)
;
path_bytes += strlen(path[npath].procname) + 1;

/*
* For each path component, allocate a 2-element ctl_table array.
Expand All @@ -956,24 +958,27 @@ struct ctl_table_header *__register_sysctl_paths(
* We allocate everything in one go so that we don't have to
* worry about freeing additional memory in unregister_sysctl_table.
*/
header = kzalloc(sizeof(struct ctl_table_header) +
header = kzalloc(sizeof(struct ctl_table_header) + path_bytes +
(2 * npath * sizeof(struct ctl_table)), GFP_KERNEL);
if (!header)
return NULL;

new = (struct ctl_table *) (header + 1);
new_name = (char *)(new + (2 * npath));

/* Now connect the dots */
prevp = &header->ctl_table;
for (n = 0; n < npath; ++n, ++path) {
/* Copy the procname */
new->procname = path->procname;
strcpy(new_name, path->procname);
new->procname = new_name;
new->mode = 0555;

*prevp = new;
prevp = &new->child;

new += 2;
new_name += strlen(new_name) + 1;
}
*prevp = table;
header->ctl_table_arg = table;
Expand Down

0 comments on commit f05e53a

Please sign in to comment.