linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] proc: Fix integer overflow of VmLib
@ 2017-01-04 23:29 Richard Weinberger
  2017-01-05  8:38 ` Vlastimil Babka
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Richard Weinberger @ 2017-01-04 23:29 UTC (permalink / raw)
  To: linux-kernel
  Cc: akpm, mhocko, vbabka, kirill.shutemov, jmarchan, gerald.schaefer,
	hannes, luto, Richard Weinberger

/proc/<pid>/status can report extremely high VmLib values which
will confuse monitoring tools.
VmLib is mm->exec_vm minus text size, where exec_vm is the number of
bytes backed by an executable memory mapping and text size is
mm->end_code - mm->start_code as set up by binfmt.

For the vast majority of all programs text size is smaller than exec_vm.
But if a program interprets binaries on its own the calculation result
can be negative.
UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
mappings but mm->start_code and mm->start_code remain and VmLib turns
negative.

Fix this by detecting the overflow and just return 0.
For interpreting the value reported by VmLib is anyway useless but
returning 0 does at least not confuse userspace.

Signed-off-by: Richard Weinberger <richard@nod.at>
---
 fs/proc/task_mmu.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 8f96a49178d0..220091c29aa6 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -46,6 +46,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
 
 	text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
 	lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
+	if ((long)lib < 0)
+		lib = 0;
 	swap = get_mm_counter(mm, MM_SWAPENTS);
 	ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
 	pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
-- 
2.10.2

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-04 23:29 [PATCH] proc: Fix integer overflow of VmLib Richard Weinberger
@ 2017-01-05  8:38 ` Vlastimil Babka
  2017-01-05 10:53 ` Michal Hocko
  2017-01-05 12:01 ` Jerome Marchand
  2 siblings, 0 replies; 9+ messages in thread
From: Vlastimil Babka @ 2017-01-05  8:38 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel
  Cc: akpm, mhocko, kirill.shutemov, jmarchan, gerald.schaefer, hannes, luto

On 01/05/2017 12:29 AM, Richard Weinberger wrote:
> /proc/<pid>/status can report extremely high VmLib values which
> will confuse monitoring tools.
> VmLib is mm->exec_vm minus text size, where exec_vm is the number of
> bytes backed by an executable memory mapping and text size is
> mm->end_code - mm->start_code as set up by binfmt.
> 
> For the vast majority of all programs text size is smaller than exec_vm.
> But if a program interprets binaries on its own the calculation result
> can be negative.
> UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
> mappings but mm->start_code and mm->start_code remain and VmLib turns
> negative.
> 
> Fix this by detecting the overflow and just return 0.
> For interpreting the value reported by VmLib is anyway useless but
> returning 0 does at least not confuse userspace.
> 
> Signed-off-by: Richard Weinberger <richard@nod.at>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>  fs/proc/task_mmu.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 8f96a49178d0..220091c29aa6 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -46,6 +46,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
>  
>  	text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
>  	lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
> +	if ((long)lib < 0)
> +		lib = 0;
>  	swap = get_mm_counter(mm, MM_SWAPENTS);
>  	ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
>  	pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-04 23:29 [PATCH] proc: Fix integer overflow of VmLib Richard Weinberger
  2017-01-05  8:38 ` Vlastimil Babka
@ 2017-01-05 10:53 ` Michal Hocko
  2017-01-05 11:03   ` Richard Weinberger
  2017-01-05 12:01 ` Jerome Marchand
  2 siblings, 1 reply; 9+ messages in thread
From: Michal Hocko @ 2017-01-05 10:53 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

I guess you meant s@overflow@underflow@ right?

On Thu 05-01-17 00:29:18, Richard Weinberger wrote:
> /proc/<pid>/status can report extremely high VmLib values which
> will confuse monitoring tools.
> VmLib is mm->exec_vm minus text size, where exec_vm is the number of
> bytes backed by an executable memory mapping and text size is
> mm->end_code - mm->start_code as set up by binfmt.
> 
> For the vast majority of all programs text size is smaller than exec_vm.
> But if a program interprets binaries on its own the calculation result
> can be negative.
> UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
> mappings but mm->start_code and mm->start_code remain and VmLib turns
> negative.
> 
> Fix this by detecting the overflow and just return 0.
> For interpreting the value reported by VmLib is anyway useless but
> returning 0 does at least not confuse userspace.

Is really 0 what the userspace expects? Why shouldn't we just report
exec_vm unconditionally? Btw. we used to do something that many years
back https://lkml.org/lkml/2004/8/24/47. We are exporting the text size
so the calculation can be done by the userspace.
 
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  fs/proc/task_mmu.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 8f96a49178d0..220091c29aa6 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -46,6 +46,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
>  
>  	text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
>  	lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
> +	if ((long)lib < 0)
> +		lib = 0;
>  	swap = get_mm_counter(mm, MM_SWAPENTS);
>  	ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
>  	pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
> -- 
> 2.10.2
> 

-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-05 10:53 ` Michal Hocko
@ 2017-01-05 11:03   ` Richard Weinberger
  2017-01-05 11:49     ` Michal Hocko
  0 siblings, 1 reply; 9+ messages in thread
From: Richard Weinberger @ 2017-01-05 11:03 UTC (permalink / raw)
  To: Michal Hocko
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

Michal,

Am 05.01.2017 um 11:53 schrieb Michal Hocko:
> I guess you meant s@overflow@underflow@ right?

Yep, of course.

> On Thu 05-01-17 00:29:18, Richard Weinberger wrote:
>> /proc/<pid>/status can report extremely high VmLib values which
>> will confuse monitoring tools.
>> VmLib is mm->exec_vm minus text size, where exec_vm is the number of
>> bytes backed by an executable memory mapping and text size is
>> mm->end_code - mm->start_code as set up by binfmt.
>>
>> For the vast majority of all programs text size is smaller than exec_vm.
>> But if a program interprets binaries on its own the calculation result
>> can be negative.
>> UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
>> mappings but mm->start_code and mm->start_code remain and VmLib turns
>> negative.
>>
>> Fix this by detecting the overflow and just return 0.
>> For interpreting the value reported by VmLib is anyway useless but
>> returning 0 does at least not confuse userspace.
> 
> Is really 0 what the userspace expects? Why shouldn't we just report
> exec_vm unconditionally? Btw. we used to do something that many years
> back https://lkml.org/lkml/2004/8/24/47. We are exporting the text size
> so the calculation can be done by the userspace.

Strictly speaking both values, 0 and exec_vm are wrong.
Userspace expects VmLib to be 0 when an application has no libs loaded,
i.e. for statically linked binaries.

So, either we report 0 as "I don't know" or exec_vm, which is also wrong.
I thought 0 is the better choice since it will not lead to wrong results
when userspace tools compute the sum of values reported by /proc/<pid>/status.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-05 11:03   ` Richard Weinberger
@ 2017-01-05 11:49     ` Michal Hocko
  2017-01-05 13:20       ` Richard Weinberger
  0 siblings, 1 reply; 9+ messages in thread
From: Michal Hocko @ 2017-01-05 11:49 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

On Thu 05-01-17 12:03:47, Richard Weinberger wrote:
> Michal,
> 
> Am 05.01.2017 um 11:53 schrieb Michal Hocko:
> > I guess you meant s@overflow@underflow@ right?
> 
> Yep, of course.
> 
> > On Thu 05-01-17 00:29:18, Richard Weinberger wrote:
> >> /proc/<pid>/status can report extremely high VmLib values which
> >> will confuse monitoring tools.
> >> VmLib is mm->exec_vm minus text size, where exec_vm is the number of
> >> bytes backed by an executable memory mapping and text size is
> >> mm->end_code - mm->start_code as set up by binfmt.
> >>
> >> For the vast majority of all programs text size is smaller than exec_vm.
> >> But if a program interprets binaries on its own the calculation result
> >> can be negative.
> >> UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
> >> mappings but mm->start_code and mm->start_code remain and VmLib turns
> >> negative.
> >>
> >> Fix this by detecting the overflow and just return 0.
> >> For interpreting the value reported by VmLib is anyway useless but
> >> returning 0 does at least not confuse userspace.
> > 
> > Is really 0 what the userspace expects? Why shouldn't we just report
> > exec_vm unconditionally? Btw. we used to do something that many years
> > back https://lkml.org/lkml/2004/8/24/47. We are exporting the text size
> > so the calculation can be done by the userspace.
> 
> Strictly speaking both values, 0 and exec_vm are wrong.
> Userspace expects VmLib to be 0 when an application has no libs loaded,
> i.e. for statically linked binaries.
> 
> So, either we report 0 as "I don't know" or exec_vm, which is also wrong.

Yes unfortunately.

> I thought 0 is the better choice since it will not lead to wrong results
> when userspace tools compute the sum of values reported by /proc/<pid>/status.

Dunno. If somebody translates 0 to statically linked library then it
could be wrong.

That being said, the underflow is _clearly_ wrong. I am not sure what
the right way is to fix this but whatever we do it might just break
somebody's usecase. Sad...
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-04 23:29 [PATCH] proc: Fix integer overflow of VmLib Richard Weinberger
  2017-01-05  8:38 ` Vlastimil Babka
  2017-01-05 10:53 ` Michal Hocko
@ 2017-01-05 12:01 ` Jerome Marchand
  2 siblings, 0 replies; 9+ messages in thread
From: Jerome Marchand @ 2017-01-05 12:01 UTC (permalink / raw)
  To: Richard Weinberger, linux-kernel
  Cc: akpm, mhocko, vbabka, kirill.shutemov, gerald.schaefer, hannes, luto


[-- Attachment #1.1: Type: text/plain, Size: 1794 bytes --]

On 01/05/2017 12:29 AM, Richard Weinberger wrote:
> /proc/<pid>/status can report extremely high VmLib values which
> will confuse monitoring tools.
> VmLib is mm->exec_vm minus text size, where exec_vm is the number of
> bytes backed by an executable memory mapping and text size is
> mm->end_code - mm->start_code as set up by binfmt.
> 
> For the vast majority of all programs text size is smaller than exec_vm.
> But if a program interprets binaries on its own the calculation result
> can be negative.
> UserModeLinux is such an example. It installs and removes lots of PROT_EXEC
> mappings but mm->start_code and mm->start_code remain and VmLib turns
> negative.
> 
> Fix this by detecting the overflow and just return 0.
> For interpreting the value reported by VmLib is anyway useless but
> returning 0 does at least not confuse userspace.

I guess returning 0 in such case is good enough, but the description of
VmLib in Documentations/filesystems/proc.txt should be updated to warn
users not to rely too much on this value.

Jerome

> 
> Signed-off-by: Richard Weinberger <richard@nod.at>
> ---
>  fs/proc/task_mmu.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 8f96a49178d0..220091c29aa6 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -46,6 +46,8 @@ void task_mem(struct seq_file *m, struct mm_struct *mm)
>  
>  	text = (PAGE_ALIGN(mm->end_code) - (mm->start_code & PAGE_MASK)) >> 10;
>  	lib = (mm->exec_vm << (PAGE_SHIFT-10)) - text;
> +	if ((long)lib < 0)
> +		lib = 0;
>  	swap = get_mm_counter(mm, MM_SWAPENTS);
>  	ptes = PTRS_PER_PTE * sizeof(pte_t) * atomic_long_read(&mm->nr_ptes);
>  	pmds = PTRS_PER_PMD * sizeof(pmd_t) * mm_nr_pmds(mm);
> 



[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-05 11:49     ` Michal Hocko
@ 2017-01-05 13:20       ` Richard Weinberger
  2017-01-05 13:49         ` Michal Hocko
  0 siblings, 1 reply; 9+ messages in thread
From: Richard Weinberger @ 2017-01-05 13:20 UTC (permalink / raw)
  To: Michal Hocko
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

Michal,

Am 05.01.2017 um 12:49 schrieb Michal Hocko:
>> I thought 0 is the better choice since it will not lead to wrong results
>> when userspace tools compute the sum of values reported by /proc/<pid>/status.
> 
> Dunno. If somebody translates 0 to statically linked library then it
> could be wrong.

Checking VmLib for 0 is not the correct way to detect a statically linked
program.
Unless I misread the code, VmLib will honour any PROT_EXEC mapping.
So, a statically linked JIT will have VmLib > 0.

Thanks,
//richard

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-05 13:20       ` Richard Weinberger
@ 2017-01-05 13:49         ` Michal Hocko
  2017-01-06  0:11           ` Richard Weinberger
  0 siblings, 1 reply; 9+ messages in thread
From: Michal Hocko @ 2017-01-05 13:49 UTC (permalink / raw)
  To: Richard Weinberger
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

On Thu 05-01-17 14:20:22, Richard Weinberger wrote:
> Michal,
> 
> Am 05.01.2017 um 12:49 schrieb Michal Hocko:
> >> I thought 0 is the better choice since it will not lead to wrong results
> >> when userspace tools compute the sum of values reported by /proc/<pid>/status.
> > 
> > Dunno. If somebody translates 0 to statically linked library then it
> > could be wrong.
> 
> Checking VmLib for 0 is not the correct way to detect a statically linked
> program.

If you just read the documentation:
VmLib                       size of shared library code

then 0 might suggest there are no shared libraries used and the code is
statically linked

> Unless I misread the code, VmLib will honour any PROT_EXEC mapping.
> So, a statically linked JIT will have VmLib > 0.

yes the code behaves differently and that's why I've said that the
reported number is not correct no matter how.

Anyway, as I've said I do not see any solution without risk of
regression while the current code is clearly wrong. If the general
consensus is that 0 is better than explicitly documenting VmLib as the
size of executable code and report it that way then I have no objections
and won't stay in the way. I am not sure which poison is worse.
-- 
Michal Hocko
SUSE Labs

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] proc: Fix integer overflow of VmLib
  2017-01-05 13:49         ` Michal Hocko
@ 2017-01-06  0:11           ` Richard Weinberger
  0 siblings, 0 replies; 9+ messages in thread
From: Richard Weinberger @ 2017-01-06  0:11 UTC (permalink / raw)
  To: Michal Hocko
  Cc: linux-kernel, akpm, vbabka, kirill.shutemov, jmarchan,
	gerald.schaefer, hannes, luto

Michal,

Am 05.01.2017 um 14:49 schrieb Michal Hocko:
> If you just read the documentation:
> VmLib                       size of shared library code
> 
> then 0 might suggest there are no shared libraries used and the code is
> statically linked

Which is IMHO not correct. So, the documentation needs a fix too.

>> Unless I misread the code, VmLib will honour any PROT_EXEC mapping.
>> So, a statically linked JIT will have VmLib > 0.
> 
> yes the code behaves differently and that's why I've said that the
> reported number is not correct no matter how.
> 
> Anyway, as I've said I do not see any solution without risk of
> regression while the current code is clearly wrong. If the general
> consensus is that 0 is better than explicitly documenting VmLib as the
> size of executable code and report it that way then I have no objections
> and won't stay in the way. I am not sure which poison is worse.
> 

Agreed. :-)

Thanks,
//richard

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-01-06  0:11 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-04 23:29 [PATCH] proc: Fix integer overflow of VmLib Richard Weinberger
2017-01-05  8:38 ` Vlastimil Babka
2017-01-05 10:53 ` Michal Hocko
2017-01-05 11:03   ` Richard Weinberger
2017-01-05 11:49     ` Michal Hocko
2017-01-05 13:20       ` Richard Weinberger
2017-01-05 13:49         ` Michal Hocko
2017-01-06  0:11           ` Richard Weinberger
2017-01-05 12:01 ` Jerome Marchand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).