Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 |
| 3 | * |
| 4 | * Author: Eric Biederman <ebiederm@xmision.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation, version 2 of the |
| 9 | * License. |
| 10 | */ |
| 11 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 13 | #include <linux/uts.h> |
| 14 | #include <linux/utsname.h> |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 15 | #include <linux/sysctl.h> |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 16 | #include <linux/wait.h> |
Ingo Molnar | cd9c513 | 2017-02-08 18:51:58 +0100 | [diff] [blame] | 17 | #include <linux/rwsem.h> |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 18 | |
Yuanhan Liu | cd89f46 | 2013-02-27 17:05:22 -0800 | [diff] [blame] | 19 | #ifdef CONFIG_PROC_SYSCTL |
| 20 | |
Jann Horn | 42a0cc3 | 2018-06-25 18:34:10 +0200 | [diff] [blame] | 21 | static void *get_uts(struct ctl_table *table) |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 22 | { |
| 23 | char *which = table->data; |
Pavel Emelyanov | 32df81c | 2007-11-28 16:21:38 -0800 | [diff] [blame] | 24 | struct uts_namespace *uts_ns; |
| 25 | |
| 26 | uts_ns = current->nsproxy->uts_ns; |
| 27 | which = (which - (char *)&init_uts_ns) + (char *)uts_ns; |
Cedric Le Goater | 7d69a1f | 2007-07-15 23:40:58 -0700 | [diff] [blame] | 28 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 29 | return which; |
| 30 | } |
| 31 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 32 | /* |
| 33 | * Special case of dostring for the UTS structure. This has locks |
| 34 | * to observe. Should this be in kernel/sys.c ???? |
| 35 | */ |
Joe Perches | 6f8fd1d | 2014-06-06 14:38:08 -0700 | [diff] [blame] | 36 | static int proc_do_uts_string(struct ctl_table *table, int write, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 37 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 38 | { |
| 39 | struct ctl_table uts_table; |
| 40 | int r; |
Jann Horn | 42a0cc3 | 2018-06-25 18:34:10 +0200 | [diff] [blame] | 41 | char tmp_data[__NEW_UTS_LEN + 1]; |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 42 | |
Jann Horn | 42a0cc3 | 2018-06-25 18:34:10 +0200 | [diff] [blame] | 43 | memcpy(&uts_table, table, sizeof(uts_table)); |
| 44 | uts_table.data = tmp_data; |
| 45 | |
| 46 | /* |
| 47 | * Buffer the value in tmp_data so that proc_dostring() can be called |
| 48 | * without holding any locks. |
| 49 | * We also need to read the original value in the write==1 case to |
| 50 | * support partial writes. |
| 51 | */ |
| 52 | down_read(&uts_sem); |
| 53 | memcpy(tmp_data, get_uts(table), sizeof(tmp_data)); |
| 54 | up_read(&uts_sem); |
| 55 | r = proc_dostring(&uts_table, write, buffer, lenp, ppos); |
| 56 | |
| 57 | if (write) { |
| 58 | /* |
| 59 | * Write back the new value. |
| 60 | * Note that, since we dropped uts_sem, the result can |
| 61 | * theoretically be incorrect if there are two parallel writes |
| 62 | * at non-zero offsets to the same sysctl. |
| 63 | */ |
| 64 | down_write(&uts_sem); |
| 65 | memcpy(get_uts(table), tmp_data, sizeof(tmp_data)); |
| 66 | up_write(&uts_sem); |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 67 | proc_sys_poll_notify(table->poll); |
Jann Horn | 42a0cc3 | 2018-06-25 18:34:10 +0200 | [diff] [blame] | 68 | } |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 69 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 70 | return r; |
| 71 | } |
| 72 | #else |
| 73 | #define proc_do_uts_string NULL |
| 74 | #endif |
| 75 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 76 | static DEFINE_CTL_TABLE_POLL(hostname_poll); |
| 77 | static DEFINE_CTL_TABLE_POLL(domainname_poll); |
| 78 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 79 | static struct ctl_table uts_kern_table[] = { |
| 80 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 81 | .procname = "ostype", |
| 82 | .data = init_uts_ns.name.sysname, |
| 83 | .maxlen = sizeof(init_uts_ns.name.sysname), |
| 84 | .mode = 0444, |
| 85 | .proc_handler = proc_do_uts_string, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 86 | }, |
| 87 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 88 | .procname = "osrelease", |
| 89 | .data = init_uts_ns.name.release, |
| 90 | .maxlen = sizeof(init_uts_ns.name.release), |
| 91 | .mode = 0444, |
| 92 | .proc_handler = proc_do_uts_string, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 93 | }, |
| 94 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 95 | .procname = "version", |
| 96 | .data = init_uts_ns.name.version, |
| 97 | .maxlen = sizeof(init_uts_ns.name.version), |
| 98 | .mode = 0444, |
| 99 | .proc_handler = proc_do_uts_string, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 100 | }, |
| 101 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 102 | .procname = "hostname", |
| 103 | .data = init_uts_ns.name.nodename, |
| 104 | .maxlen = sizeof(init_uts_ns.name.nodename), |
| 105 | .mode = 0644, |
| 106 | .proc_handler = proc_do_uts_string, |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 107 | .poll = &hostname_poll, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 108 | }, |
| 109 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 110 | .procname = "domainname", |
| 111 | .data = init_uts_ns.name.domainname, |
| 112 | .maxlen = sizeof(init_uts_ns.name.domainname), |
| 113 | .mode = 0644, |
| 114 | .proc_handler = proc_do_uts_string, |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 115 | .poll = &domainname_poll, |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 116 | }, |
| 117 | {} |
| 118 | }; |
| 119 | |
| 120 | static struct ctl_table uts_root_table[] = { |
| 121 | { |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 122 | .procname = "kernel", |
| 123 | .mode = 0555, |
| 124 | .child = uts_kern_table, |
| 125 | }, |
| 126 | {} |
| 127 | }; |
| 128 | |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 129 | #ifdef CONFIG_PROC_SYSCTL |
| 130 | /* |
| 131 | * Notify userspace about a change in a certain entry of uts_kern_table, |
| 132 | * identified by the parameter proc. |
| 133 | */ |
| 134 | void uts_proc_notify(enum uts_proc proc) |
| 135 | { |
| 136 | struct ctl_table *table = &uts_kern_table[proc]; |
| 137 | |
| 138 | proc_sys_poll_notify(table->poll); |
| 139 | } |
| 140 | #endif |
| 141 | |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 142 | static int __init utsname_sysctl_init(void) |
| 143 | { |
Eric W. Biederman | 0b4d414 | 2007-02-14 00:34:09 -0800 | [diff] [blame] | 144 | register_sysctl_table(uts_root_table); |
Eric W. Biederman | 39732ac | 2007-02-14 00:33:58 -0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Fabian Frederick | 95583e4 | 2014-06-04 16:11:26 -0700 | [diff] [blame] | 148 | device_initcall(utsname_sysctl_init); |