utsname_sysctl.c 3.5 KB

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