123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706 |
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/syscalls.h>
- #include <linux/unistd.h>
- #include <linux/kmod.h>
- #include <linux/slab.h>
- #include <linux/completion.h>
- #include <linux/cred.h>
- #include <linux/file.h>
- #include <linux/fdtable.h>
- #include <linux/workqueue.h>
- #include <linux/security.h>
- #include <linux/mount.h>
- #include <linux/kernel.h>
- #include <linux/init.h>
- #include <linux/resource.h>
- #include <linux/notifier.h>
- #include <linux/suspend.h>
- #include <linux/rwsem.h>
- #include <linux/ptrace.h>
- #include <linux/async.h>
- #include <asm/uaccess.h>
- #include <trace/events/module.h>
- extern int max_threads;
- #define CAP_BSET (void *)1
- #define CAP_PI (void *)2
- static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
- static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
- static DEFINE_SPINLOCK(umh_sysctl_lock);
- static DECLARE_RWSEM(umhelper_sem);
- #ifdef CONFIG_MODULES
- char modprobe_path[KMOD_PATH_LEN] = "/sbin/modprobe";
- static void free_modprobe_argv(struct subprocess_info *info)
- {
- kfree(info->argv[3]);
- kfree(info->argv);
- }
- static int call_modprobe(char *module_name, int wait)
- {
- struct subprocess_info *info;
- static char *envp[] = {
- "HOME=/",
- "TERM=linux",
- "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
- NULL
- };
- char **argv = kmalloc(sizeof(char *[5]), GFP_KERNEL);
- if (!argv)
- goto out;
- module_name = kstrdup(module_name, GFP_KERNEL);
- if (!module_name)
- goto free_argv;
- argv[0] = modprobe_path;
- argv[1] = "-q";
- argv[2] = "--";
- argv[3] = module_name;
- argv[4] = NULL;
- info = call_usermodehelper_setup(modprobe_path, argv, envp, GFP_KERNEL,
- NULL, free_modprobe_argv, NULL);
- if (!info)
- goto free_module_name;
- return call_usermodehelper_exec(info, wait | UMH_KILLABLE);
- free_module_name:
- kfree(module_name);
- free_argv:
- kfree(argv);
- out:
- return -ENOMEM;
- }
- int __request_module(bool wait, const char *fmt, ...)
- {
- va_list args;
- char module_name[MODULE_NAME_LEN];
- unsigned int max_modprobes;
- int ret;
- static atomic_t kmod_concurrent = ATOMIC_INIT(0);
- #define MAX_KMOD_CONCURRENT 50
- static int kmod_loop_msg;
-
- WARN_ON_ONCE(wait && current_is_async());
- if (!modprobe_path[0])
- return 0;
- va_start(args, fmt);
- ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args);
- va_end(args);
- if (ret >= MODULE_NAME_LEN)
- return -ENAMETOOLONG;
- ret = security_kernel_module_request(module_name);
- if (ret)
- return ret;
-
- max_modprobes = min(max_threads/2, MAX_KMOD_CONCURRENT);
- atomic_inc(&kmod_concurrent);
- if (atomic_read(&kmod_concurrent) > max_modprobes) {
-
- if (kmod_loop_msg < 5) {
- printk(KERN_ERR
- "request_module: runaway loop modprobe %s\n",
- module_name);
- kmod_loop_msg++;
- }
- atomic_dec(&kmod_concurrent);
- return -ENOMEM;
- }
- trace_module_request(module_name, wait, _RET_IP_);
- ret = call_modprobe(module_name, wait ? UMH_WAIT_PROC : UMH_WAIT_EXEC);
- atomic_dec(&kmod_concurrent);
- return ret;
- }
- EXPORT_SYMBOL(__request_module);
- #endif
- static void call_usermodehelper_freeinfo(struct subprocess_info *info)
- {
- if (info->cleanup)
- (*info->cleanup)(info);
- kfree(info);
- }
- static void umh_complete(struct subprocess_info *sub_info)
- {
- struct completion *comp = xchg(&sub_info->complete, NULL);
-
- if (comp)
- complete(comp);
- else
- call_usermodehelper_freeinfo(sub_info);
- }
- static int call_usermodehelper_exec_async(void *data)
- {
- struct subprocess_info *sub_info = data;
- struct cred *new;
- int retval;
- spin_lock_irq(¤t->sighand->siglock);
- flush_signal_handlers(current, 1);
- spin_unlock_irq(¤t->sighand->siglock);
-
- set_user_nice(current, 0);
- retval = -ENOMEM;
- new = prepare_kernel_cred(current);
- if (!new)
- goto out;
- spin_lock(&umh_sysctl_lock);
- new->cap_bset = cap_intersect(usermodehelper_bset, new->cap_bset);
- new->cap_inheritable = cap_intersect(usermodehelper_inheritable,
- new->cap_inheritable);
- spin_unlock(&umh_sysctl_lock);
- if (sub_info->init) {
- retval = sub_info->init(sub_info, new);
- if (retval) {
- abort_creds(new);
- goto out;
- }
- }
- commit_creds(new);
- retval = do_execve(getname_kernel(sub_info->path),
- (const char __user *const __user *)sub_info->argv,
- (const char __user *const __user *)sub_info->envp);
- out:
- sub_info->retval = retval;
-
- if (!(sub_info->wait & UMH_WAIT_PROC))
- umh_complete(sub_info);
- if (!retval)
- return 0;
- do_exit(0);
- }
- static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
- {
- pid_t pid;
-
- kernel_sigaction(SIGCHLD, SIG_DFL);
- pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
- if (pid < 0) {
- sub_info->retval = pid;
- } else {
- int ret = -ECHILD;
-
- sys_wait4(pid, (int __user *)&ret, 0, NULL);
-
- if (ret)
- sub_info->retval = ret;
- }
-
- kernel_sigaction(SIGCHLD, SIG_IGN);
- umh_complete(sub_info);
- }
- static void call_usermodehelper_exec_work(struct work_struct *work)
- {
- struct subprocess_info *sub_info =
- container_of(work, struct subprocess_info, work);
- if (sub_info->wait & UMH_WAIT_PROC) {
- call_usermodehelper_exec_sync(sub_info);
- } else {
- pid_t pid;
-
- pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
- CLONE_PARENT | SIGCHLD);
- if (pid < 0) {
- sub_info->retval = pid;
- umh_complete(sub_info);
- }
- }
- }
- static enum umh_disable_depth usermodehelper_disabled = UMH_DISABLED;
- static atomic_t running_helpers = ATOMIC_INIT(0);
- static DECLARE_WAIT_QUEUE_HEAD(running_helpers_waitq);
- static DECLARE_WAIT_QUEUE_HEAD(usermodehelper_disabled_waitq);
- #define RUNNING_HELPERS_TIMEOUT (5 * HZ)
- int usermodehelper_read_trylock(void)
- {
- DEFINE_WAIT(wait);
- int ret = 0;
- down_read(&umhelper_sem);
- for (;;) {
- prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
- TASK_INTERRUPTIBLE);
- if (!usermodehelper_disabled)
- break;
- if (usermodehelper_disabled == UMH_DISABLED)
- ret = -EAGAIN;
- up_read(&umhelper_sem);
- if (ret)
- break;
- schedule();
- try_to_freeze();
- down_read(&umhelper_sem);
- }
- finish_wait(&usermodehelper_disabled_waitq, &wait);
- return ret;
- }
- EXPORT_SYMBOL_GPL(usermodehelper_read_trylock);
- long usermodehelper_read_lock_wait(long timeout)
- {
- DEFINE_WAIT(wait);
- if (timeout < 0)
- return -EINVAL;
- down_read(&umhelper_sem);
- for (;;) {
- prepare_to_wait(&usermodehelper_disabled_waitq, &wait,
- TASK_UNINTERRUPTIBLE);
- if (!usermodehelper_disabled)
- break;
- up_read(&umhelper_sem);
- timeout = schedule_timeout(timeout);
- if (!timeout)
- break;
- down_read(&umhelper_sem);
- }
- finish_wait(&usermodehelper_disabled_waitq, &wait);
- return timeout;
- }
- EXPORT_SYMBOL_GPL(usermodehelper_read_lock_wait);
- void usermodehelper_read_unlock(void)
- {
- up_read(&umhelper_sem);
- }
- EXPORT_SYMBOL_GPL(usermodehelper_read_unlock);
- void __usermodehelper_set_disable_depth(enum umh_disable_depth depth)
- {
- down_write(&umhelper_sem);
- usermodehelper_disabled = depth;
- wake_up(&usermodehelper_disabled_waitq);
- up_write(&umhelper_sem);
- }
- int __usermodehelper_disable(enum umh_disable_depth depth)
- {
- long retval;
- if (!depth)
- return -EINVAL;
- down_write(&umhelper_sem);
- usermodehelper_disabled = depth;
- up_write(&umhelper_sem);
-
- retval = wait_event_timeout(running_helpers_waitq,
- atomic_read(&running_helpers) == 0,
- RUNNING_HELPERS_TIMEOUT);
- if (retval)
- return 0;
- __usermodehelper_set_disable_depth(UMH_ENABLED);
- return -EAGAIN;
- }
- static void helper_lock(void)
- {
- atomic_inc(&running_helpers);
- smp_mb__after_atomic();
- }
- static void helper_unlock(void)
- {
- if (atomic_dec_and_test(&running_helpers))
- wake_up(&running_helpers_waitq);
- }
- struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
- char **envp, gfp_t gfp_mask,
- int (*init)(struct subprocess_info *info, struct cred *new),
- void (*cleanup)(struct subprocess_info *info),
- void *data)
- {
- struct subprocess_info *sub_info;
- sub_info = kzalloc(sizeof(struct subprocess_info), gfp_mask);
- if (!sub_info)
- goto out;
- INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
- sub_info->path = path;
- sub_info->argv = argv;
- sub_info->envp = envp;
- sub_info->cleanup = cleanup;
- sub_info->init = init;
- sub_info->data = data;
- out:
- return sub_info;
- }
- EXPORT_SYMBOL(call_usermodehelper_setup);
- int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
- {
- DECLARE_COMPLETION_ONSTACK(done);
- int retval = 0;
- if (!sub_info->path) {
- call_usermodehelper_freeinfo(sub_info);
- return -EINVAL;
- }
- helper_lock();
- if (usermodehelper_disabled) {
- retval = -EBUSY;
- goto out;
- }
-
- sub_info->complete = (wait == UMH_NO_WAIT) ? NULL : &done;
- sub_info->wait = wait;
- queue_work(system_unbound_wq, &sub_info->work);
- if (wait == UMH_NO_WAIT)
- goto unlock;
- if (wait & UMH_KILLABLE) {
- retval = wait_for_completion_killable(&done);
- if (!retval)
- goto wait_done;
-
- if (xchg(&sub_info->complete, NULL))
- goto unlock;
-
- }
- wait_for_completion(&done);
- wait_done:
- retval = sub_info->retval;
- out:
- call_usermodehelper_freeinfo(sub_info);
- unlock:
- helper_unlock();
- return retval;
- }
- EXPORT_SYMBOL(call_usermodehelper_exec);
- int call_usermodehelper(char *path, char **argv, char **envp, int wait)
- {
- struct subprocess_info *info;
- gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
- info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
- NULL, NULL, NULL);
- if (info == NULL)
- return -ENOMEM;
- return call_usermodehelper_exec(info, wait);
- }
- EXPORT_SYMBOL(call_usermodehelper);
- static int proc_cap_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- struct ctl_table t;
- unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
- kernel_cap_t new_cap;
- int err, i;
- if (write && (!capable(CAP_SETPCAP) ||
- !capable(CAP_SYS_MODULE)))
- return -EPERM;
-
- spin_lock(&umh_sysctl_lock);
- for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++) {
- if (table->data == CAP_BSET)
- cap_array[i] = usermodehelper_bset.cap[i];
- else if (table->data == CAP_PI)
- cap_array[i] = usermodehelper_inheritable.cap[i];
- else
- BUG();
- }
- spin_unlock(&umh_sysctl_lock);
- t = *table;
- t.data = &cap_array;
-
- err = proc_doulongvec_minmax(&t, write, buffer, lenp, ppos);
- if (err < 0)
- return err;
-
- for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
- new_cap.cap[i] = cap_array[i];
-
- spin_lock(&umh_sysctl_lock);
- if (write) {
- if (table->data == CAP_BSET)
- usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
- if (table->data == CAP_PI)
- usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
- }
- spin_unlock(&umh_sysctl_lock);
- return 0;
- }
- struct ctl_table usermodehelper_table[] = {
- {
- .procname = "bset",
- .data = CAP_BSET,
- .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
- .mode = 0600,
- .proc_handler = proc_cap_handler,
- },
- {
- .procname = "inheritable",
- .data = CAP_PI,
- .maxlen = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
- .mode = 0600,
- .proc_handler = proc_cap_handler,
- },
- { }
- };
|