Subject: Re: [PATCH V9] RO/NX protection for loadable kernel modules

From: Rusty Russell <[email-masked]>
Date: Tue, 2 Feb 2010 08:19:55 +1030

On Tue, 2 Feb 2010 02:59:41 am Siarhei Liakh wrote:
> V9: coding style fixes

But not compile tested:

> +void set_page_attributes(void *start, void *end,
> +		void (*set)(unsigned long start, int num_pages))
> +{
> +	unsigned long begin_pfn = PFN_DOWN((unsigned long)start);
> +	unsigned long end_pfn = PFN_DOWN((unsigned long)end);
> +	if (end_pfn > begin_pfn)
> +		set(begin_pfn << PAGE_SHIFT, end_pfn - begin_pfn);
> +}
...
> +	if (ro_size > 0) {
> +		set_page_attributes((unsigned long)base,
> +					(unsigned long)base + ro_size,
> +					set_memory_ro);

The casts here should cause a warning. 

> +	if (total_size > text_size) {
> +		begin_pfn = PFN_UP((unsigned long)base + text_size);
> +		end_pfn = PFN_UP((unsigned long)base + total_size);
> +		if (end_pfn > begin_pfn)
> +			set_memory_nx(begin_pfn << PAGE_SHIFT,
> +						end_pfn - begin_pfn);

Use the helper here too?

> +		if ((mod->module_core) && (mod->core_text_size)) {
> +			set_page_attributes((unsigned long)mod->module_core,
> +						(unsigned long)mod->module_core
> +							+ mod->core_text_size,
> +						set_memory_rw);
> +		}
> +		if ((mod->module_init) && (mod->init_text_size)) {
> +			set_page_attributes((unsigned long)mod->module_init,
> +						(unsigned long)mod->module_init
> +							+ mod->init_text_size,
> +						set_memory_rw);

More unwanted casts...

> +			set_page_attributes((unsigned long)mod->module_core,
> +						(unsigned long)mod->module_core
> +							+ mod->core_text_size,
> +						set_memory_ro);
> +		}
> +		if ((mod->module_init) && (mod->init_text_size)) {
> +			set_page_attributes((unsigned long)mod->module_init,
> +						(unsigned long)mod->module_init
> +							+ mod->init_text_size,
> +						set_memory_ro);

Here, too.

Thanks,
Rusty.