op = (write ? 002 : 004); if (ctl_perm(table, op)) goto out; /* careful: calling conventions are nasty here */ res = count; error = (*table->proc_handler)(table, write, file, buf, &res, ppos); if (!error) error = res; out: spin_lock(&sysctl_lock); unuse_table(de->set); } spin_unlock(&sysctl_lock); return error;} static int proc_opensys(struct inode *inode, struct file *file){ if (file->f_mode & FMODE_WRITE) { /* * sysctl entries that are not writable, * are _NOT_ writable, capabilities or not. */ if (!(inode->i_mode & S_IWUSR)) return -EPERM; } return 0;} static ssize_t proc_readsys(struct file * file, char __user * buf, size_t count, loff_t *ppos){ return do_rw_proc(0, file, buf, count, ppos);} static ssize_t proc_writesys(struct file * file, const char __user * buf, size_t count, loff_t *ppos){ return do_rw_proc(1, file, (char __user *) buf, count, ppos);} /** * proc_dostring - read a string sysctl * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes a string from/to the user buffer. If the kernel * buffer provided is not large enough to hold the string, the * string is truncated. The copied string is %NULL-terminated. * If the string is being read by the user process, it is copied * and a newline '\n' is added. It is truncated if the buffer is * not large enough. * * Returns 0 on success. */int proc_dostring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ size_t len; char __user *p; char c; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } if (write) { len = 0; p = buffer; while (len < *lenp) { if (get_user(c, p++)) return -EFAULT; if (c == 0 || c == '\n') break; len++; } if (len >= table->maxlen) len = table->maxlen-1; if(copy_from_user(table->data, buffer, len)) return -EFAULT; ((char *) table->data)[len] = 0; *ppos += *lenp; } else { len = strlen(table->data); if (len > table->maxlen) len = table->maxlen; if (len > *lenp) len = *lenp; if (len) if(copy_to_user(buffer, table->data, len)) return -EFAULT; if (len < *lenp) { if(put_user('\n', ((char __user *) buffer) + len)) return -EFAULT; len++; } *lenp = len; *ppos += len; } return 0;} /* * Special case of dostring for the UTS structure. This has locks * to observe. Should this be in kernel/sys.c ???? */ static int proc_doutsstring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ int r; if (!write) { down_read(&uts_sem); r=proc_dostring(table,0,filp,buffer,lenp, ppos); up_read(&uts_sem); } else { down_write(&uts_sem); r=proc_dostring(table,1,filp,buffer,lenp, ppos); up_write(&uts_sem); } return r;} static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ if (write) { *valp = *negp ? -*lvalp : *lvalp; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;} static int do_proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, int (*conv)(int *negp, unsigned long *lvalp, int *valp, int write, void *data), void *data){#define TMPBUFLEN 21 int *i, vleft, first=1, neg, val; unsigned long lval; size_t left, len; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (int *) table->data; vleft = table->maxlen / sizeof(*i); left = *lenp; if (!conv) conv = do_proc_dointvec_conv; for (; left && vleft--; i++, first=0) { if (write) { while (left) { char c; if (get_user(c, s)) return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > sizeof(buf) - 1) len = sizeof(buf) - 1; if (copy_from_user(buf, s, len)) return -EFAULT; buf[len] = 0; p = buf; if (*p == '-' && left > 1) { neg = 1; left--, p++; } if (*p < '0' || *p > '9') break; lval = simple_strtoul(p, &p, 0); len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if (conv(&neg, &lval, i, 1, data)) break; } else { p = buf; if (!first) *p++ = '\t'; if (conv(&neg, &lval, i, 0, data)) break; sprintf(p, "%s%lu", neg ? "-" : "", lval); len = strlen(buf); if (len > left) len = left; if(copy_to_user(s, buf, len)) return -EFAULT; left -= len; s += len; } } if (!write && !first && left) { if(put_user('\n', s)) return -EFAULT; left--, s++; } if (write) { while (left) { char c; if (get_user(c, s++)) return -EFAULT; if (!isspace(c)) break; left--; } } if (write && first) return -EINVAL; *lenp -= left; *ppos += *lenp; return 0;#undef TMPBUFLEN} /** * proc_dointvec - read a vector of integers * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * Returns 0 on success. */int proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, NULL,NULL);} #define OP_SET 0#define OP_AND 1#define OP_OR 2#define OP_MAX 3#define OP_MIN 4 static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ int op = *(int *)data; if (write) { int val = *negp ? -*lvalp : *lvalp; switch(op) { case OP_SET: *valp = val; break; case OP_AND: *valp &= val; break; case OP_OR: *valp |= val; break; case OP_MAX: if(*valp < val) *valp = val; break; case OP_MIN: if(*valp > val) *valp = val; break; } } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;} /* * init may raise the set. */ int proc_dointvec_bset(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ int op; if (!capable(CAP_SYS_MODULE)) { return -EPERM; } op = (current->pid == 1) ? OP_SET : OP_AND; return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, do_proc_dointvec_bset_conv,&op);} struct do_proc_dointvec_minmax_conv_param { int *min; int *max;}; static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data){ struct do_proc_dointvec_minmax_conv_param *param = data; if (write) { int val = *negp ? -*lvalp : *lvalp; if ((param->min && *param->min > val) || (param->max && *param->max < val)) return -EINVAL; *valp = val; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0;} /** * proc_dointvec_minmax - read a vector of integers with min/max values * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * This routine will ensure the values are within the range specified by * table->extra1 (min) and table->extra2 (max). * * Returns 0 on success. */int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos){ struct do_proc_dointvec_minmax_conv_param param = { .min = (int *) table->extra1, .max = (int *) table->extra2, }; return do_proc_dointvec(table, write, filp, buffer, lenp, ppos, do_proc_dointvec_minmax_conv, ¶m);} static int do_proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul, unsigned long convdiv){#define TMPBUFLEN 21 unsigned long *i, *min, *max, val; int vleft, first=1, neg; size_t len, left; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (unsigned long *) table->data; min = (unsigned long *) table->extra1; max = (unsigned long *) table->extra2; vleft = table->maxlen / sizeof(unsigned long); left = *lenp; for (; left && vleft--; i++, min++, max++, first=0) { if (write) { while (left) { char c; if (get_user(c, s)) return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > TMPBUFLEN-1) len = TMPBUFLEN-1; if (copy_from_user(buf, s, len)) return -EFAULT; buf[len] = 0; p = buf; if (*p == '-' && left > 1) { neg = 1; left--, p++; } if (*p < '0' || *p > '9') break; val = simple_strtoul(p, &p, 0) * convmul / convdiv ; len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if(neg) continue; if ((min && val < *min) || (max && val > *max)) continue; *i = val; } else { p = buf; if (!first) *p++ = '\t'; sprintf(p, "%lu", convdiv * (*i) / convmul); len = strlen(buf); if (len > left) len = left; if(copy_to_user(s, buf, len)) return -EFAULT; left -= len; s += len; } } if (!write && !first && left) { if(put_user('\n', s)) return -EFAULT; left--, s++; } if (write) { while (left) { char c; if (get_user(c, s++)) return -EFAULT; if (!isspace(c)) break; left--; } } if (write && first) return -EINVAL; *lenp -= left; *ppos += *lenp; return 0;#undef TMPBUFLEN} /**Lines 1501–2000 of 2519. Previous lines Load more
op = (write ? 002 : 004); if (ctl_perm(table, op)) goto out; /* careful: calling conventions are nasty here */ res = count; error = (*table->proc_handler)(table, write, file, buf, &res, ppos); if (!error) error = res; out: spin_lock(&sysctl_lock); unuse_table(de->set); } spin_unlock(&sysctl_lock); return error; } static int proc_opensys(struct inode *inode, struct file *file) { if (file->f_mode & FMODE_WRITE) { /* * sysctl entries that are not writable, * are _NOT_ writable, capabilities or not. */ if (!(inode->i_mode & S_IWUSR)) return -EPERM; } return 0; } static ssize_t proc_readsys(struct file * file, char __user * buf, size_t count, loff_t *ppos) { return do_rw_proc(0, file, buf, count, ppos); } static ssize_t proc_writesys(struct file * file, const char __user * buf, size_t count, loff_t *ppos) { return do_rw_proc(1, file, (char __user *) buf, count, ppos); } /** * proc_dostring - read a string sysctl * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes a string from/to the user buffer. If the kernel * buffer provided is not large enough to hold the string, the * string is truncated. The copied string is %NULL-terminated. * If the string is being read by the user process, it is copied * and a newline '\n' is added. It is truncated if the buffer is * not large enough. * * Returns 0 on success. */ int proc_dostring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) { size_t len; char __user *p; char c; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } if (write) { len = 0; p = buffer; while (len < *lenp) { if (get_user(c, p++)) return -EFAULT; if (c == 0 || c == '\n') break; len++; } if (len >= table->maxlen) len = table->maxlen-1; if(copy_from_user(table->data, buffer, len)) return -EFAULT; ((char *) table->data)[len] = 0; *ppos += *lenp; } else { len = strlen(table->data); if (len > table->maxlen) len = table->maxlen; if (len > *lenp) len = *lenp; if (len) if(copy_to_user(buffer, table->data, len)) return -EFAULT; if (len < *lenp) { if(put_user('\n', ((char __user *) buffer) + len)) return -EFAULT; len++; } *lenp = len; *ppos += len; } return 0; } /* * Special case of dostring for the UTS structure. This has locks * to observe. Should this be in kernel/sys.c ???? */ static int proc_doutsstring(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) { int r; if (!write) { down_read(&uts_sem); r=proc_dostring(table,0,filp,buffer,lenp, ppos); up_read(&uts_sem); } else { down_write(&uts_sem); r=proc_dostring(table,1,filp,buffer,lenp, ppos); up_write(&uts_sem); } return r; } static int do_proc_dointvec_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data) { if (write) { *valp = *negp ? -*lvalp : *lvalp; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0; } static int do_proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, int (*conv)(int *negp, unsigned long *lvalp, int *valp, int write, void *data), void *data) { #define TMPBUFLEN 21 int *i, vleft, first=1, neg, val; unsigned long lval; size_t left, len; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (int *) table->data; vleft = table->maxlen / sizeof(*i); left = *lenp; if (!conv) conv = do_proc_dointvec_conv; for (; left && vleft--; i++, first=0) { if (write) { while (left) { char c; if (get_user(c, s)) return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > sizeof(buf) - 1) len = sizeof(buf) - 1; if (copy_from_user(buf, s, len)) return -EFAULT; buf[len] = 0; p = buf; if (*p == '-' && left > 1) { neg = 1; left--, p++; } if (*p < '0' || *p > '9') break; lval = simple_strtoul(p, &p, 0); len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if (conv(&neg, &lval, i, 1, data)) break; } else { p = buf; if (!first) *p++ = '\t'; if (conv(&neg, &lval, i, 0, data)) break; sprintf(p, "%s%lu", neg ? "-" : "", lval); len = strlen(buf); if (len > left) len = left; if(copy_to_user(s, buf, len)) return -EFAULT; left -= len; s += len; } } if (!write && !first && left) { if(put_user('\n', s)) return -EFAULT; left--, s++; } if (write) { while (left) { char c; if (get_user(c, s++)) return -EFAULT; if (!isspace(c)) break; left--; } } if (write && first) return -EINVAL; *lenp -= left; *ppos += *lenp; return 0; #undef TMPBUFLEN } /** * proc_dointvec - read a vector of integers * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * Returns 0 on success. */ int proc_dointvec(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) { return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, NULL,NULL); } #define OP_SET 0 #define OP_AND 1 #define OP_OR 2 #define OP_MAX 3 #define OP_MIN 4 static int do_proc_dointvec_bset_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data) { int op = *(int *)data; if (write) { int val = *negp ? -*lvalp : *lvalp; switch(op) { case OP_SET: *valp = val; break; case OP_AND: *valp &= val; break; case OP_OR: *valp |= val; break; case OP_MAX: if(*valp < val) *valp = val; break; case OP_MIN: if(*valp > val) *valp = val; break; } } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0; } /* * init may raise the set. */ int proc_dointvec_bset(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) { int op; if (!capable(CAP_SYS_MODULE)) { return -EPERM; } op = (current->pid == 1) ? OP_SET : OP_AND; return do_proc_dointvec(table,write,filp,buffer,lenp,ppos, do_proc_dointvec_bset_conv,&op); } struct do_proc_dointvec_minmax_conv_param { int *min; int *max; }; static int do_proc_dointvec_minmax_conv(int *negp, unsigned long *lvalp, int *valp, int write, void *data) { struct do_proc_dointvec_minmax_conv_param *param = data; if (write) { int val = *negp ? -*lvalp : *lvalp; if ((param->min && *param->min > val) || (param->max && *param->max < val)) return -EINVAL; *valp = val; } else { int val = *valp; if (val < 0) { *negp = -1; *lvalp = (unsigned long)-val; } else { *negp = 0; *lvalp = (unsigned long)val; } } return 0; } /** * proc_dointvec_minmax - read a vector of integers with min/max values * @table: the sysctl table * @write: %TRUE if this is a write to the sysctl file * @filp: the file structure * @buffer: the user buffer * @lenp: the size of the user buffer * @ppos: file position * * Reads/writes up to table->maxlen/sizeof(unsigned int) integer * values from/to the user buffer, treated as an ASCII string. * * This routine will ensure the values are within the range specified by * table->extra1 (min) and table->extra2 (max). * * Returns 0 on success. */ int proc_dointvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos) { struct do_proc_dointvec_minmax_conv_param param = { .min = (int *) table->extra1, .max = (int *) table->extra2, }; return do_proc_dointvec(table, write, filp, buffer, lenp, ppos, do_proc_dointvec_minmax_conv, ¶m); } static int do_proc_doulongvec_minmax(ctl_table *table, int write, struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos, unsigned long convmul, unsigned long convdiv) { #define TMPBUFLEN 21 unsigned long *i, *min, *max, val; int vleft, first=1, neg; size_t len, left; char buf[TMPBUFLEN], *p; char __user *s = buffer; if (!table->data || !table->maxlen || !*lenp || (*ppos && !write)) { *lenp = 0; return 0; } i = (unsigned long *) table->data; min = (unsigned long *) table->extra1; max = (unsigned long *) table->extra2; vleft = table->maxlen / sizeof(unsigned long); left = *lenp; for (; left && vleft--; i++, min++, max++, first=0) { if (write) { while (left) { char c; if (get_user(c, s)) return -EFAULT; if (!isspace(c)) break; left--; s++; } if (!left) break; neg = 0; len = left; if (len > TMPBUFLEN-1) len = TMPBUFLEN-1; if (copy_from_user(buf, s, len)) return -EFAULT; buf[len] = 0; p = buf; if (*p == '-' && left > 1) { neg = 1; left--, p++; } if (*p < '0' || *p > '9') break; val = simple_strtoul(p, &p, 0) * convmul / convdiv ; len = p-buf; if ((len < left) && *p && !isspace(*p)) break; if (neg) val = -val; s += len; left -= len; if(neg) continue; if ((min && val < *min) || (max && val > *max)) continue; *i = val; } else { p = buf; if (!first) *p++ = '\t'; sprintf(p, "%lu", convdiv * (*i) / convmul); len = strlen(buf); if (len > left) len = left; if(copy_to_user(s, buf, len)) return -EFAULT; left -= len; s += len; } } if (!write && !first && left) { if(put_user('\n', s)) return -EFAULT; left--, s++; } if (write) { while (left) { char c; if (get_user(c, s++)) return -EFAULT; if (!isspace(c)) break; left--; } } if (write && first) return -EINVAL; *lenp -= left; *ppos += *lenp; return 0; #undef TMPBUFLEN } /**