Skip to contentSkip to conversation

proc: Fix integer overflow of VmLib

PATCH In review Richard Weinberger 4 participants 9 messages Jan 4, 2017 – Jan 6, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 proc: Fix integer overflow of VmLib

/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.

---
 fs/proc/task_mmu.c | 2 ++
 1 file changed, 2 insertions(+)
fs/proc/task_mmu.c +2 -0
index 8f96a49178d0..220091c29aa6 100644
@@ -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);
Signature · 1 line
2.10.2
Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · 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.

Quoted 5 lines

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.

Quoted 18 lines
---
 fs/proc/task_mmu.c | 2 ++
 1 file changed, 2 insertions(+)
fs/proc/task_mmu.c +2 -0
index 8f96a49178d0..220091c29aa6 100644
@@ -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 lore raw
In reply to Richard Weinberger

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

Quoted 13 lines · 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.

Quoted 3 lines

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.

Quoted 21 lines
---
 fs/proc/task_mmu.c | 2 ++
 1 file changed, 2 insertions(+)
fs/proc/task_mmu.c +2 -0
index 8f96a49178d0..220091c29aa6 100644
@@ -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);
Signature · 1 line
2.10.2
Signature · 2 lines
Michal Hocko
SUSE Labs
#3 lore raw

Michal,

Am 05.01.2017 um 11:53 schrieb Michal Hocko:

Quoted 1 line

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

Yep, of course.

Quoted 14 lines · Richard Weinberger
Quoted 13 lines

/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.

Quoted 8 lines
Quoted 3 lines

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

#4 lore raw
Quoted 30 lines · Richard Weinberger

Michal,

Am 05.01.2017 um 11:53 schrieb Michal Hocko:

Quoted 1 line

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

Yep, of course.

Quoted 14 lines · Richard Weinberger
Quoted 13 lines

/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.

Quoted 8 lines
Quoted 3 lines

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.

Quoted 5 lines

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.

Quoted 2 lines

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...

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw

Michal,

Am 05.01.2017 um 12:49 schrieb Michal Hocko:

Quoted 5 lines
Quoted 2 lines

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

#6 lore raw
Quoted 11 lines · Richard Weinberger

Michal,

Am 05.01.2017 um 12:49 schrieb Michal Hocko:

Quoted 5 lines
Quoted 2 lines

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

Quoted 2 lines

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.

Signature · 2 lines
Michal Hocko
SUSE Labs
#7 lore raw

Michal,

Am 05.01.2017 um 14:49 schrieb Michal Hocko:

Quoted 5 lines

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.

Quoted 12 lines
Quoted 2 lines

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

#8 lore raw
In reply to Richard Weinberger
Quoted 13 lines · 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.

Quoted 3 lines

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

Quoted 20 lines
---
 fs/proc/task_mmu.c | 2 ++
 1 file changed, 2 insertions(+)
fs/proc/task_mmu.c +2 -0
index 8f96a49178d0..220091c29aa6 100644
@@ -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);