8080# define debug_align (X ) (X)
8181#endif
8282
83- /*
84- * Given BASE and SIZE this macro calculates the number of pages the
85- * memory regions occupies
86- */
87- #define MOD_NUMBER_OF_PAGES (BASE , SIZE ) (((SIZE) > 0) ? \
88- (PFN_DOWN((unsigned long)(BASE) + (SIZE) - 1) - \
89- PFN_DOWN((unsigned long)BASE) + 1) \
90- : (0UL))
91-
9283/* If this is set, the section belongs in the init part of the module */
9384#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
9485
@@ -1858,74 +1849,75 @@ static void mod_sysfs_teardown(struct module *mod)
18581849/*
18591850 * LKM RO/NX protection: protect module's text/ro-data
18601851 * from modification and any data from execution.
1852+ *
1853+ * General layout of module is:
1854+ * [text] [read-only-data] [writable data]
1855+ * text_size -----^ ^ ^
1856+ * ro_size ------------------------| |
1857+ * size -------------------------------------------|
1858+ *
1859+ * These values are always page-aligned (as is base)
18611860 */
1862- void set_page_attributes (void * start , void * end , int (* set )(unsigned long start , int num_pages ))
1861+ static void frob_text (const struct module_layout * layout ,
1862+ int (* set_memory )(unsigned long start , int num_pages ))
18631863{
1864- unsigned long begin_pfn = PFN_DOWN ((unsigned long )start );
1865- unsigned long end_pfn = PFN_DOWN ((unsigned long )end );
1866-
1867- if (end_pfn > begin_pfn )
1868- set (begin_pfn << PAGE_SHIFT , end_pfn - begin_pfn );
1864+ BUG_ON ((unsigned long )layout -> base & (PAGE_SIZE - 1 ));
1865+ BUG_ON ((unsigned long )layout -> text_size & (PAGE_SIZE - 1 ));
1866+ set_memory ((unsigned long )layout -> base ,
1867+ layout -> text_size >> PAGE_SHIFT );
18691868}
18701869
1871- static void set_section_ro_nx (void * base ,
1872- unsigned long text_size ,
1873- unsigned long ro_size ,
1874- unsigned long total_size ,
1875- int (* set_ro )(unsigned long start , int num_pages ),
1876- int (* set_nx )(unsigned long start , int num_pages ))
1870+ static void frob_rodata (const struct module_layout * layout ,
1871+ int (* set_memory )(unsigned long start , int num_pages ))
18771872{
1878- /* begin and end PFNs of the current subsection */
1879- unsigned long begin_pfn ;
1880- unsigned long end_pfn ;
1881-
1882- /*
1883- * Set RO for module text and RO-data:
1884- * - Always protect first page.
1885- * - Do not protect last partial page.
1886- */
1887- if (ro_size > 0 )
1888- set_page_attributes (base , base + ro_size , set_ro );
1873+ BUG_ON ((unsigned long )layout -> base & (PAGE_SIZE - 1 ));
1874+ BUG_ON ((unsigned long )layout -> text_size & (PAGE_SIZE - 1 ));
1875+ BUG_ON ((unsigned long )layout -> ro_size & (PAGE_SIZE - 1 ));
1876+ set_memory ((unsigned long )layout -> base + layout -> text_size ,
1877+ (layout -> ro_size - layout -> text_size ) >> PAGE_SHIFT );
1878+ }
18891879
1890- /*
1891- * Set NX permissions for module data:
1892- * - Do not protect first partial page.
1893- * - Always protect last page.
1894- */
1895- if (total_size > text_size ) {
1896- begin_pfn = PFN_UP ((unsigned long )base + text_size );
1897- end_pfn = PFN_UP ((unsigned long )base + total_size );
1898- if (end_pfn > begin_pfn )
1899- set_nx (begin_pfn << PAGE_SHIFT , end_pfn - begin_pfn );
1900- }
1880+ static void frob_writable_data (const struct module_layout * layout ,
1881+ int (* set_memory )(unsigned long start , int num_pages ))
1882+ {
1883+ BUG_ON ((unsigned long )layout -> base & (PAGE_SIZE - 1 ));
1884+ BUG_ON ((unsigned long )layout -> ro_size & (PAGE_SIZE - 1 ));
1885+ BUG_ON ((unsigned long )layout -> size & (PAGE_SIZE - 1 ));
1886+ set_memory ((unsigned long )layout -> base + layout -> ro_size ,
1887+ (layout -> size - layout -> ro_size ) >> PAGE_SHIFT );
19011888}
19021889
1903- static void set_module_core_ro_nx (struct module * mod )
1890+ /* livepatching wants to disable read-only so it can frob module. */
1891+ void module_disable_ro (const struct module * mod )
19041892{
1905- set_section_ro_nx (mod -> core_layout .base , mod -> core_layout .text_size ,
1906- mod -> core_layout .ro_size , mod -> core_layout .size ,
1907- set_memory_ro , set_memory_nx );
1893+ frob_text (& mod -> core_layout , set_memory_rw );
1894+ frob_rodata (& mod -> core_layout , set_memory_rw );
1895+ frob_text (& mod -> init_layout , set_memory_rw );
1896+ frob_rodata (& mod -> init_layout , set_memory_rw );
19081897}
19091898
1910- static void unset_module_core_ro_nx ( struct module * mod )
1899+ void module_enable_ro ( const struct module * mod )
19111900{
1912- set_section_ro_nx (mod -> core_layout .base , mod -> core_layout .text_size ,
1913- mod -> core_layout .ro_size , mod -> core_layout .size ,
1914- set_memory_rw , set_memory_x );
1901+ frob_text (& mod -> core_layout , set_memory_ro );
1902+ frob_rodata (& mod -> core_layout , set_memory_ro );
1903+ frob_text (& mod -> init_layout , set_memory_ro );
1904+ frob_rodata (& mod -> init_layout , set_memory_ro );
19151905}
19161906
1917- static void set_module_init_ro_nx ( struct module * mod )
1907+ static void module_enable_nx ( const struct module * mod )
19181908{
1919- set_section_ro_nx (mod -> init_layout .base , mod -> init_layout .text_size ,
1920- mod -> init_layout .ro_size , mod -> init_layout .size ,
1921- set_memory_ro , set_memory_nx );
1909+ frob_rodata (& mod -> core_layout , set_memory_nx );
1910+ frob_writable_data (& mod -> core_layout , set_memory_nx );
1911+ frob_rodata (& mod -> init_layout , set_memory_nx );
1912+ frob_writable_data (& mod -> init_layout , set_memory_nx );
19221913}
19231914
1924- static void unset_module_init_ro_nx ( struct module * mod )
1915+ static void module_disable_nx ( const struct module * mod )
19251916{
1926- set_section_ro_nx (mod -> init_layout .base , mod -> init_layout .text_size ,
1927- mod -> init_layout .ro_size , mod -> init_layout .size ,
1928- set_memory_rw , set_memory_x );
1917+ frob_rodata (& mod -> core_layout , set_memory_x );
1918+ frob_writable_data (& mod -> core_layout , set_memory_x );
1919+ frob_rodata (& mod -> init_layout , set_memory_x );
1920+ frob_writable_data (& mod -> init_layout , set_memory_x );
19291921}
19301922
19311923/* Iterate through all modules and set each module's text as RW */
@@ -1937,16 +1929,9 @@ void set_all_modules_text_rw(void)
19371929 list_for_each_entry_rcu (mod , & modules , list ) {
19381930 if (mod -> state == MODULE_STATE_UNFORMED )
19391931 continue ;
1940- if ((mod -> core_layout .base ) && (mod -> core_layout .text_size )) {
1941- set_page_attributes (mod -> core_layout .base ,
1942- mod -> core_layout .base + mod -> core_layout .text_size ,
1943- set_memory_rw );
1944- }
1945- if ((mod -> init_layout .base ) && (mod -> init_layout .text_size )) {
1946- set_page_attributes (mod -> init_layout .base ,
1947- mod -> init_layout .base + mod -> init_layout .text_size ,
1948- set_memory_rw );
1949- }
1932+
1933+ frob_text (& mod -> core_layout , set_memory_rw );
1934+ frob_text (& mod -> init_layout , set_memory_rw );
19501935 }
19511936 mutex_unlock (& module_mutex );
19521937}
@@ -1960,24 +1945,25 @@ void set_all_modules_text_ro(void)
19601945 list_for_each_entry_rcu (mod , & modules , list ) {
19611946 if (mod -> state == MODULE_STATE_UNFORMED )
19621947 continue ;
1963- if ((mod -> core_layout .base ) && (mod -> core_layout .text_size )) {
1964- set_page_attributes (mod -> core_layout .base ,
1965- mod -> core_layout .base + mod -> core_layout .text_size ,
1966- set_memory_ro );
1967- }
1968- if ((mod -> init_layout .base ) && (mod -> init_layout .text_size )) {
1969- set_page_attributes (mod -> init_layout .base ,
1970- mod -> init_layout .base + mod -> init_layout .text_size ,
1971- set_memory_ro );
1972- }
1948+
1949+ frob_text (& mod -> core_layout , set_memory_ro );
1950+ frob_text (& mod -> init_layout , set_memory_ro );
19731951 }
19741952 mutex_unlock (& module_mutex );
19751953}
1954+
1955+ static void disable_ro_nx (const struct module_layout * layout )
1956+ {
1957+ frob_text (layout , set_memory_rw );
1958+ frob_rodata (layout , set_memory_rw );
1959+ frob_rodata (layout , set_memory_x );
1960+ frob_writable_data (layout , set_memory_x );
1961+ }
1962+
19761963#else
1977- static void set_module_core_ro_nx (struct module * mod ) { }
1978- static void set_module_init_ro_nx (struct module * mod ) { }
1979- static void unset_module_core_ro_nx (struct module * mod ) { }
1980- static void unset_module_init_ro_nx (struct module * mod ) { }
1964+ static void disable_ro_nx (const struct module_layout * layout ) { }
1965+ static void module_enable_nx (const struct module * mod ) { }
1966+ static void module_disable_nx (const struct module * mod ) { }
19811967#endif
19821968
19831969void __weak module_memfree (void * module_region )
@@ -2029,8 +2015,8 @@ static void free_module(struct module *mod)
20292015 synchronize_sched ();
20302016 mutex_unlock (& module_mutex );
20312017
2032- /* This may be NULL , but that's OK */
2033- unset_module_init_ro_nx ( mod );
2018+ /* This may be empty , but that's OK */
2019+ disable_ro_nx ( & mod -> init_layout );
20342020 module_arch_freeing_init (mod );
20352021 module_memfree (mod -> init_layout .base );
20362022 kfree (mod -> args );
@@ -2040,7 +2026,7 @@ static void free_module(struct module *mod)
20402026 lockdep_free_key_range (mod -> core_layout .base , mod -> core_layout .size );
20412027
20422028 /* Finally, free the core (containing the module structure) */
2043- unset_module_core_ro_nx ( mod );
2029+ disable_ro_nx ( & mod -> core_layout );
20442030 module_memfree (mod -> core_layout .base );
20452031
20462032#ifdef CONFIG_MPU
@@ -3275,7 +3261,7 @@ static noinline int do_init_module(struct module *mod)
32753261 mod -> strtab = mod -> core_strtab ;
32763262#endif
32773263 mod_tree_remove_init (mod );
3278- unset_module_init_ro_nx ( mod );
3264+ disable_ro_nx ( & mod -> init_layout );
32793265 module_arch_freeing_init (mod );
32803266 mod -> init_layout .base = NULL ;
32813267 mod -> init_layout .size = 0 ;
@@ -3370,8 +3356,8 @@ static int complete_formation(struct module *mod, struct load_info *info)
33703356 module_bug_finalize (info -> hdr , info -> sechdrs , mod );
33713357
33723358 /* Set RO and NX regions */
3373- set_module_init_ro_nx (mod );
3374- set_module_core_ro_nx (mod );
3359+ module_enable_ro (mod );
3360+ module_enable_nx (mod );
33753361
33763362 /* Mark state as coming so strong_try_module_get() ignores us,
33773363 * but kallsyms etc. can see us. */
@@ -3536,8 +3522,8 @@ static int load_module(struct load_info *info, const char __user *uargs,
35363522 MODULE_STATE_GOING , mod );
35373523
35383524 /* we can't deallocate the module until we clear memory protection */
3539- unset_module_init_ro_nx (mod );
3540- unset_module_core_ro_nx (mod );
3525+ module_disable_ro (mod );
3526+ module_disable_nx (mod );
35413527
35423528 ddebug_cleanup :
35433529 dynamic_debug_remove (info -> debug );
0 commit comments