1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * Copyright (C) 2007 2 * Copyright (C) 2007 4 * 3 * 5 * Author: Eric Biederman <ebiederm@xmision.c 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. 6 */ 10 */ 7 11 8 #include <linux/export.h> 12 #include <linux/export.h> 9 #include <linux/uts.h> 13 #include <linux/uts.h> 10 #include <linux/utsname.h> 14 #include <linux/utsname.h> 11 #include <linux/random.h> << 12 #include <linux/sysctl.h> 15 #include <linux/sysctl.h> 13 #include <linux/wait.h> 16 #include <linux/wait.h> 14 #include <linux/rwsem.h> 17 #include <linux/rwsem.h> 15 18 16 #ifdef CONFIG_PROC_SYSCTL 19 #ifdef CONFIG_PROC_SYSCTL 17 20 18 static void *get_uts(const struct ctl_table *t !! 21 static void *get_uts(struct ctl_table *table, int write) 19 { 22 { 20 char *which = table->data; 23 char *which = table->data; 21 struct uts_namespace *uts_ns; 24 struct uts_namespace *uts_ns; 22 25 23 uts_ns = current->nsproxy->uts_ns; 26 uts_ns = current->nsproxy->uts_ns; 24 which = (which - (char *)&init_uts_ns) 27 which = (which - (char *)&init_uts_ns) + (char *)uts_ns; 25 28 >> 29 if (!write) >> 30 down_read(&uts_sem); >> 31 else >> 32 down_write(&uts_sem); 26 return which; 33 return which; 27 } 34 } 28 35 >> 36 static void put_uts(struct ctl_table *table, int write, void *which) >> 37 { >> 38 if (!write) >> 39 up_read(&uts_sem); >> 40 else >> 41 up_write(&uts_sem); >> 42 } >> 43 29 /* 44 /* 30 * Special case of dostring for the UTS s 45 * Special case of dostring for the UTS structure. This has locks 31 * to observe. Should this be in kernel/s 46 * to observe. Should this be in kernel/sys.c ???? 32 */ 47 */ 33 static int proc_do_uts_string(const struct ctl !! 48 static int proc_do_uts_string(struct ctl_table *table, int write, 34 void *buffer, size_t *lenp, !! 49 void __user *buffer, size_t *lenp, loff_t *ppos) 35 { 50 { 36 struct ctl_table uts_table; 51 struct ctl_table uts_table; 37 int r; 52 int r; 38 char tmp_data[__NEW_UTS_LEN + 1]; << 39 << 40 memcpy(&uts_table, table, sizeof(uts_t 53 memcpy(&uts_table, table, sizeof(uts_table)); 41 uts_table.data = tmp_data; !! 54 uts_table.data = get_uts(table, write); 42 << 43 /* << 44 * Buffer the value in tmp_data so tha << 45 * without holding any locks. << 46 * We also need to read the original v << 47 * support partial writes. << 48 */ << 49 down_read(&uts_sem); << 50 memcpy(tmp_data, get_uts(table), sizeo << 51 up_read(&uts_sem); << 52 r = proc_dostring(&uts_table, write, b 55 r = proc_dostring(&uts_table, write, buffer, lenp, ppos); >> 56 put_uts(table, write, uts_table.data); 53 57 54 if (write) { !! 58 if (write) 55 /* << 56 * Write back the new value. << 57 * Note that, since we dropped << 58 * theoretically be incorrect << 59 * at non-zero offsets to the << 60 */ << 61 add_device_randomness(tmp_data << 62 down_write(&uts_sem); << 63 memcpy(get_uts(table), tmp_dat << 64 up_write(&uts_sem); << 65 proc_sys_poll_notify(table->po 59 proc_sys_poll_notify(table->poll); 66 } << 67 60 68 return r; 61 return r; 69 } 62 } 70 #else 63 #else 71 #define proc_do_uts_string NULL 64 #define proc_do_uts_string NULL 72 #endif 65 #endif 73 66 74 static DEFINE_CTL_TABLE_POLL(hostname_poll); 67 static DEFINE_CTL_TABLE_POLL(hostname_poll); 75 static DEFINE_CTL_TABLE_POLL(domainname_poll); 68 static DEFINE_CTL_TABLE_POLL(domainname_poll); 76 69 77 // Note: update 'enum uts_proc' to match any c << 78 static struct ctl_table uts_kern_table[] = { 70 static struct ctl_table uts_kern_table[] = { 79 { 71 { 80 .procname = "arch", << 81 .data = init_uts_ns. << 82 .maxlen = sizeof(init_ << 83 .mode = 0444, << 84 .proc_handler = proc_do_uts_ << 85 }, << 86 { << 87 .procname = "ostype", 72 .procname = "ostype", 88 .data = init_uts_ns. 73 .data = init_uts_ns.name.sysname, 89 .maxlen = sizeof(init_ 74 .maxlen = sizeof(init_uts_ns.name.sysname), 90 .mode = 0444, 75 .mode = 0444, 91 .proc_handler = proc_do_uts_ 76 .proc_handler = proc_do_uts_string, 92 }, 77 }, 93 { 78 { 94 .procname = "osrelease", 79 .procname = "osrelease", 95 .data = init_uts_ns. 80 .data = init_uts_ns.name.release, 96 .maxlen = sizeof(init_ 81 .maxlen = sizeof(init_uts_ns.name.release), 97 .mode = 0444, 82 .mode = 0444, 98 .proc_handler = proc_do_uts_ 83 .proc_handler = proc_do_uts_string, 99 }, 84 }, 100 { 85 { 101 .procname = "version", 86 .procname = "version", 102 .data = init_uts_ns. 87 .data = init_uts_ns.name.version, 103 .maxlen = sizeof(init_ 88 .maxlen = sizeof(init_uts_ns.name.version), 104 .mode = 0444, 89 .mode = 0444, 105 .proc_handler = proc_do_uts_ 90 .proc_handler = proc_do_uts_string, 106 }, 91 }, 107 { 92 { 108 .procname = "hostname", 93 .procname = "hostname", 109 .data = init_uts_ns. 94 .data = init_uts_ns.name.nodename, 110 .maxlen = sizeof(init_ 95 .maxlen = sizeof(init_uts_ns.name.nodename), 111 .mode = 0644, 96 .mode = 0644, 112 .proc_handler = proc_do_uts_ 97 .proc_handler = proc_do_uts_string, 113 .poll = &hostname_po 98 .poll = &hostname_poll, 114 }, 99 }, 115 { 100 { 116 .procname = "domainname" 101 .procname = "domainname", 117 .data = init_uts_ns. 102 .data = init_uts_ns.name.domainname, 118 .maxlen = sizeof(init_ 103 .maxlen = sizeof(init_uts_ns.name.domainname), 119 .mode = 0644, 104 .mode = 0644, 120 .proc_handler = proc_do_uts_ 105 .proc_handler = proc_do_uts_string, 121 .poll = &domainname_ 106 .poll = &domainname_poll, 122 }, 107 }, >> 108 {} >> 109 }; >> 110 >> 111 static struct ctl_table uts_root_table[] = { >> 112 { >> 113 .procname = "kernel", >> 114 .mode = 0555, >> 115 .child = uts_kern_table, >> 116 }, >> 117 {} 123 }; 118 }; 124 119 125 #ifdef CONFIG_PROC_SYSCTL 120 #ifdef CONFIG_PROC_SYSCTL 126 /* 121 /* 127 * Notify userspace about a change in a certai 122 * Notify userspace about a change in a certain entry of uts_kern_table, 128 * identified by the parameter proc. 123 * identified by the parameter proc. 129 */ 124 */ 130 void uts_proc_notify(enum uts_proc proc) 125 void uts_proc_notify(enum uts_proc proc) 131 { 126 { 132 struct ctl_table *table = &uts_kern_ta 127 struct ctl_table *table = &uts_kern_table[proc]; 133 128 134 proc_sys_poll_notify(table->poll); 129 proc_sys_poll_notify(table->poll); 135 } 130 } 136 #endif 131 #endif 137 132 138 static int __init utsname_sysctl_init(void) 133 static int __init utsname_sysctl_init(void) 139 { 134 { 140 register_sysctl("kernel", uts_kern_tab !! 135 register_sysctl_table(uts_root_table); 141 return 0; 136 return 0; 142 } 137 } 143 138 144 device_initcall(utsname_sysctl_init); 139 device_initcall(utsname_sysctl_init); 145 140
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.