Commit Graph

101 Commits

Author SHA1 Message Date
Craig Small
f3e5290012 pmap: minor Coverity fix for -N option
99126 Explicit null dereferenced
Not 100% sure this is valid (the same branch that sets the variable
is the one that sets N_option) but not too hard to fix.

Signed-off-by: Craig Small <csmall@dropbear.xyz>
2021-11-02 17:14:29 +11:00
Jan Rybar
d293b686e4 Coverity scan findings - memleaks, unused vars, potential nullptr dereferences 2021-05-10 20:09:20 +10:00
Jim Warner
2a3997e2f0 misc: needed adaptations for the changes in <pids> api
That snowball, which began as a simple removal of some
brackets, now ends with this third patch restoring the
ability to build our project. It was made necessary by
the renaming (and rearranging) of several enumerators.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2021-03-11 21:21:23 +11:00
Jim Warner
1aa8b16441 library: eliminated the questionable 'procps.h' header
There was a time when that procps.h file served a more
traditional role. Prior to the commit referenced below
it held just macros plus manifest constants. But, with
that change, such items were replaced with a series of
includes embracing all the library exported functions.

That approach was known to disguise errors which would
have otherwise yielded a compiler warning. And without
such a warning, there was no way to address the error.

So this patch will trade the all inclusive header file
approach for individual includes only where necessary.

Reference(s):
. April 2016, procps.h header file revamped
commit ccb6ae8de1
. Sept 2018, top abandoned use of procps.h
commit a6dfc2382e

Signed-off-by: Jim Warner <james.warner@comcast.net>
2020-07-05 21:13:01 +10:00
Qualys Security Advisory
7bc4779718 0095-pmap: Fix extended mode in one_proc().
Check the return value of sscanf() to make sure that all input items are
properly initialized.

In extended mode (x_option), one_proc() loads the values of start and
perms during one iteration of the while loop, and displays them during
one of the following iterations, but start and perms are variables local
to the while loop: move them out of the while loop, to the beginning of
the function.

Also, display a mapping only if cp2 is properly initialized; otherwise
(for example), mappings that do not belong to a selected range are
displayed, and with a NULL mapping name:

$ pmap -x -A 6FFF00000000,7FFF00000000 $$
...
Address           Kbytes     RSS   Dirty Mode  Mapping
000055b3d1e9b000       0     912       0  r-xp (null)
000055b3d2194000       0      16      16  r--p (null)
000055b3d2198000       0      36      36  rw-p (null)
...

Removed const as this causes problems elsewhere.

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
7d3b4bcaf2 0093-pmap: Remove dead code in mapping_name().
If "cp = strrchr(mapbuf_b, '/')" then this function returns, and
otherwise there is no '/' in mapbuf_b and "cp = strchr(mapbuf_b, '/')"
is always false: remove this second block, since it is never entered.
Also, constify a few things in this function.

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
991b41cb32 0092-pmap: Harden one_proc().
Replace sprintf() with snprintf().

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
550a2a21f9 0091-pmap: Check sscanf() in discover_shm_minor().
Need at least 6 items ("inode" is unused).

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
2119cd3dd5 0090-pmap: Fix output format of VmFlags.
In the headers, the space was misplaced; for example, "pmap -XX $$"
outputs "VmFlagsMapping" (without a space). Use justify_print() instead
of printf().

There was also an extra space in the output, because vmflags[] (from the
"VmFlags:" line) always ends with a space. Overwriting this last space
with a null byte fixes this misalignment.

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
7e2bd279ed 0089-pmap: Prevent buffer overflow in sscanf().
vmflags[] is a 27*(2+1)=81 char array, but there are 30 flags now (not
27), and even with 27 flags this was an off-by-one overflow (the kernel
always outputs a flag with "%c%c ", so the last +1 is for a space, not
for the terminating null byte). Protect vmflags[] with a maximum field
width, as in the surrounding sscanf() calls.

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
5f654f143a 0088-pmap: Always check the return value of fgets().
Otherwise "the contents of the array remain unchanged and a null pointer
is returned" or "the array contents are indeterminate and a null pointer
is returned".

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
45f81ef706 0087-pmap: Fix parsing error in config_read().
$ echo '[' > crash
$ pmap -C crash $$
Segmentation fault (core dumped)

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
bf409f92fd 0086-pmap: Prevent integer overflow in main().
Unlikely to ever happen, but just in case.

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Qualys Security Advisory
105ab093b8 0085-pmap.c: Plug memory leak in range_arguments().
Also, simplify the code slightly (but functionally equivalent). Check
the return value of xstrdup() only once (yes, it can return NULL).

Adapted slightly to remove goto and leave the format of checks the same.
A lot of the fixes were already in newlib, caught by coverity

References:
 commit 25f655891f

Signed-off-by: Craig Small <csmall@enc.com.au>
2018-06-23 21:59:14 +10:00
Jim Warner
e02d9f554d pmap: fix printing bug associated with the '-x' option
Ever since its introduction, the 'x' (extended format)
option has employed strncmp to parse those smaps keys.

Such an approach worked well as long as those prefixes
were guaranteed to be unique. But, with the 4.3 kernel
a new 'SwapPss' field was added to those within smaps.

That triggered a 2nd match for the 'Swap' logic which,
in turn, resulted in a duplicate output line of zeros.

So this patch just trades strncmp for strcmp, avoiding
potential future problems when /proc/$$/smaps evolves.

Reference(s):
. recent bug report
https://bugzilla.redhat.com/show_bug.cgi?id=1374061
. linux 4.3 kernel introduces SwapPss
commit 8334b96221ff0dcbde4873d31eb4d84774ed8ed4
. original pmap -x option introduction
commit 380cc1e908

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-09-11 09:31:05 +10:00
Jim Warner
e0515e23e7 related: respond to VAL macro addition of context parm
Since the VAL macro now requires a 4th parameter, this
commit simply adds the 'info' context structure to it.

In some cases, that context structure needed to become
global, since it was referenced in multiple functions.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-08-07 21:40:48 +10:00
Jim Warner
612f36189e related: change for lost 'PROCPS_' enumerator prefixes
With this patch we've completed a progression toward a
standard approach to naming conventions which follows:

* Only functions will begin with that 'procps_' prefix
. ........................................... examples
. procps_vmstat_get ()
. procps_diskstats_select ()
- ----------------------------------------------------

* Exposed structures begin with the module/header name
. ........................................... examples
. struct pids_info
. struct stat_reaped
- ----------------------------------------------------

* Item enumerators begin like structs, but capitalized
. ........................................... examples
. VMSTAT_COMPACT_FAIL
. MEMINFO_DELTA_ACTIVE

[ slabinfo varies slightly due to some item variants ]
. SLABINFO_extra
. SLABS_SIZE_ACTIVE
. SLABNODE_OBJS_PER_SLAB
[ could cure with a prefix of SLABINFO, but too long ]
- ----------------------------------------------------

* Other enumerators work exactly like item enumerators
. ........................................... examples
. PIDS_SORT_ASCEND
. STAT_REAP_CPUS_AND_NODES
- ----------------------------------------------------

* Macros and constants begin just like the enumerators
. ........................................... examples
. #define SLABINFO_GET
. #define DISKSTATS_TYPE_DISK
- ----------------------------------------------------

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-07-26 20:49:44 +10:00
Jim Warner
d7cbf3448f related: adapt to changes in 'context' structure names
Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-07-26 20:47:50 +10:00
Jim Warner
a2c79b6237 misc: adapt others to struct layout change, <PIDS> api
With the change to struct pids_fetch, we'll just trade
some dot ('.') code for some pointer to ('->') syntax.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-06-28 21:11:25 +10:00
Jan Rybar
2a7d3f7d70 - Fixing incorrect memory usage assessment due to skipping vmflags parsing
- Resolves Red Hat Bugzilla #1262864, affecting upstream
2016-06-17 21:58:39 +10:00
Craig Small
25f655891f pmap: Minor fixes
Some reasource leaks and a bunch of flags not explictly set.

References:
 Coverity 99162, 99146, 99145
2016-05-17 21:55:14 +10:00
Jim Warner
591ae1746c pmap: finally silence a warning without creating a bug
The patch referenced below silenced an 'uninitialized'
compiler warning but it also created a bug where zeros
appeared under the Address column with that -x option.

So this commit swats that bug and avoids any warnings.

[ while yours truly created that bug, in his defense ]
[ let's at least acknowledge the god awful loop code ]
[ which was the root of the problem & wrong solution ]

[ so the ugliness of this most recent solution is in ]
[ perfect harmony with the *really* ugly loop itself ]

Reference(s):
commit 0299bd15b0

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-05-16 19:58:20 +10:00
Jim Warner
56def1cbac misc: adapt others to changes in interface, <PIDS> api
I've got nothing to add to the commit message but that
doesn't mean I won't produce perfectly justified text.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-05-16 19:58:20 +10:00
Cristian Rodríguez
2dfab56755 pmap: Do not display error if shmctl(..IPC_RMID) returns EINVAL
The segment may have been destroyed by the kernel automagically
after shmdt(addr)

How to reproduce:

sysctl -w kernel.shm_rmid_forced=1
./pmap 1
shared memory remove: Invalid argument
[..]
2016-04-28 21:37:46 +10:00
Jim Warner
714ea69c6d misc: adapt others to a changed identifier, <pids> API
Signed-off-by: Jim Warner <james.warner@comcast.net>
2016-04-19 20:38:18 +10:00
Craig Small
ccb6ae8de1 library: cleanup of library includes
The includes used to define a lot of things a library include
should not. It was also a bit messy what was exposed in the library
and what was not.

get_pid_digits -> procps_pid_length and exported correctly

MALLOC attribute move into relevant .c files
NORETURN attribute moved to relevant .c, not used in library
PURE attribute removed, it wasn't used
KLONG/KLF/STRTOUKL were fixed for long, so now just use long

HIDDEN attribute removed. It was for 3 functions. The PROCPS_EXPORT
seems to do the same (opposite) thing.

likely/unlikely removed from most places, its highly debateable
this does anything useful as CPUs have gotten smarter about branches.

Re-arranged the includes, ALL external programs should just #include
<proc/procps.h> then proc/procps.h includes headers for files that
have exported functions. procps.h and the headers it includes should
not use items that are not exportable (e.g. hidden functions or
macros) they go in procps-private.h
2016-04-16 17:03:57 +10:00
Emanuele Aina
b921e2e765 pmap: Fix detail parsing on long mapping lines
If the mapping descriptor is longer than 128 chars, the last parsed
character won't be a newline even if the current buffer contains it a
bit further than that. The current code always interprets it as a short
fgets() read instead, and thus keeps calling fgets() until it gets a
newline, dropping valid lines and failing with the following error:

pmap: ERROR: inconsistent detail field in smaps file, line:
 Rss:                 212 kB
2016-04-11 22:14:03 +10:00
Jim Warner
a7153fe49f pmap: adapt to normailzed <pids> select/fill interface
Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-10-05 21:50:01 +11:00
Jim Warner
987f4f2e3e pmap: adapt to changes in <pids> API regarding address
This commit was prompted by that change from 'addr' to
'ul_int' in the <pids> interface. Along the way, KLONG
was removed as having long ago outlived its usefulness
as performance optimizations for weird configurations.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-09-21 22:36:36 +10:00
Jim Warner
e2898e213f pmap: modify to utilize that new procps_pids interface
Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-08-31 17:48:33 +10:00
Jim Warner
0299bd15b0 pmap: silence a clang -Wuninitialized variable warning
Reference(s):
pmap.c:618:20: warning: variable 'start' is uninitialized when used here [-Wuninitialized]
                                               maxw1, start,
                                                      ^~~~~
Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-08-31 17:47:53 +10:00
Jim Warner
8072b6aa84 pmap: correct heading misalignment of VmFlags with -XX
Signed-off-by: Jim Warner <james.warner@comcast.net>
2015-08-31 17:47:37 +10:00
Yuriy M. Kaminskiy
9ed623780f fix integer overflow on 2GiB+ maps on 32-bit platforms
Signed-off-by: Craig Small <csmall@enc.com.au>
2015-06-20 21:39:41 +10:00
Craig Small
92071e963e pmap: print process even if smaps unreadable
pmap would previously print the process name if
/proc/PID/smaps could be opened, even if subsequent
reads failed.  This actually occurs with other users
PIDs.

Kernel 3.18rc1 introduced a change where the file could
not been opened, meaning pmap -X 1 previously showed
the process name and nothing else but NOW shows nothing
make check failed because of this.

This change prints the process name even before trying to open
the file, returning it to previous behaviour.
Thanks to Vincent Bernat for some analysis.

References:
  https://bugs.debian.org/775624
  https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/commit/?id=29a40ace841cba9b661711f042d1821cdc4ad47c

Signed-off-by: Craig Small <csmall@enc.com.au>
2015-01-24 18:53:29 +11:00
Adam Sampson
1d212457f2 pmap: avoid depending on an uninitialised value.
It's possible for the first entry that this reads from /proc/*/smaps to
start with a map_desc-less line like this:

7fa71b77d000-7fa71c288000 rw-p 00000000 00:00 0

in which case sscanf will only read up to inode, and it won't set c; the
code below would then incorrectly discard the next ("Size:") line.

(With GCC 4.9.0, this bug causes the "pmap extra extended output" test
to fail for me.)
2014-08-19 18:59:28 +02:00
Jim Warner
bcbc3c5a02 misc: result after checking all files for misspellings
Reference(s):
https://github.com/lyda/misspell-check.git

Signed-off-by: Jim Warner <james.warner@comcast.net>
2014-08-08 22:14:21 +02:00
Craig Small
8e7ef322e2 Update help files
Benno Schulenberg suggested some changes to the help messages
to provide some consistency and clarity for both the users and
translators of procps.

The test needed to be updated as the pmap output changed too.

Signed-off-by: Craig Small <csmall@enc.com.au>
2014-02-02 18:13:01 +11:00
Craig Small
8a38cd5eb4 Split help lines to help translators
To assist translators, the help lines are split so that each translation
chunk has one option. This gives bonus of if we add or change an option,
only that option remains untranslated rather than the entire help block.

Reference:
  http://www.freelists.org/post/procps/procpsng-for-Translation-Project,1

Signed-off-by: Craig Small <csmall@enc.com.au>
2013-12-28 09:25:39 +11:00
Cristian Rodríguez
5a39544b21 Fix off-by-one in pmap
When procps is built with gcc 4.8 address sanitizer

static int one_proc(proc_t * p)..
..
char smap_key[20];
...
(sscanf(mapbuf, "%20[^:]: %llu", smap_key..

rightfully results in an overflow and the program aborts.
2013-05-22 18:22:37 -04:00
Craig Small
293b668d5f Merge commit 'refs/merge-requests/10' of git://gitorious.org/procps/procps into merge-requests/10 2013-04-07 17:58:06 +10:00
Gilles Espinasse
d164f47dd1 procps-ng : fix pmap uninitialized warnings
pmap.c: In function 'one_proc':
pmap.c:529: warning: 'maxw1' may be used uninitialized in this function
pmap.c:529: warning: 'maxw2' may be used uninitialized in this function
pmap.c:529: warning: 'maxw3' may be used uninitialized in this function
pmap.c:529: warning: 'maxw4' may be used uninitialized in this function
pmap.c:529: warning: 'maxw5' may be used uninitialized in this function

Without -d and -x option, that should be doable to trigger the issue but I haven't found how

Signed-off-by: Gilles Espinasse <g.esp@free.fr>
2013-03-26 21:23:28 +11:00
Gilles Espinasse
bccc2404b6 procps-ng : fix pmap unused variable warnings
pmap.c: In function 'print_extended_maps':
pmap.c:310: warning: unused variable 'value'
pmap.c: In function 'config_read':
pmap.c:792: warning: unused variable 'section'
pmap.c: In function 'get_default_rc_filename':
pmap.c:969: warning: unused variable 'ret'
pmap.c: In function 'main':
pmap.c:999: warning: unused variable 'default_rc_filename'

Signed-off-by: Gilles Espinasse <g.esp@free.fr>
2013-03-26 20:55:51 +11:00
Rainer Müller
042776e04c configure: check for program_invocation_name
For portabiliy, check for program_invocation_name during configure and
define HAVE_PROGRAM_INVOCATION_NAME accordingly. Use of this symbol is
now enclosed with the appropriate #ifdef block.

The symbol program_invocation_name is only used for error message
handling using error(), so it's safe to omit this if it is not
available.
2013-03-20 16:32:06 +01:00
Jaromir Capik
43bcb47007 pmap: Fixing -n,-N x -p,-q check
The -p,-q switches are mutually exclusive with -n,-N, but
not between each other. This commit changes the expression
to a correct one.
2013-03-19 10:47:36 +01:00
Jaromir Capik
5c513ce582 pmap: fixing the width measurement of extended_pmap with -q
This commit fixes the width measurement of the extended pmap
function to work correctly with the -q switch. With no header
and no footer only widths of the particular values matter.
2013-03-18 19:43:12 +01:00
Jim Warner
c80e93be47 pmap: a spade is a spade, so let's call a Flags a Perm
For some reason when the new -X/-XX options were added
what was always displayed as 'Mode' became 'Flags'. So
now a precious horizontal space is wasted because that
field's data has only 4 bytes whereas the header is 5.

Moreover, this created a conflict with the top program
which used that name in a column header already. Plus,
top emitted a 'Translation Hint' that suggests the max
chars should be 8 which is a limit pmap needn't share!

The xgettext program provides no way to keep identical
strings separate. This meant both top and pmap will be
sharing not only the msgid but a Translation Hint too.
So, the solution will now be two distinct field names.

Besides the kernel folks document it as 'perm' anyway!

Reference(s):
       new options for pmap
commit faec340719

Signed-off-by: Jim Warner <james.warner@comcast.net>
2013-03-18 18:42:07 +01:00
Jim Warner
0c0c543466 pmap: wield my machete, achieve width-wise nls support
The existing gettext nls support in pmap exposed users
to some potentially ugly misalignments should the text
that's used in headers someday actually be translated.

The length issue had been addressed already for -X/-XX
modes, but the column headers weren't nls translatable
as yet. This commit makes any header (not literally in
/proc/#/smaps) nls aware. It provides translated width
protection to all modes except one that's header-less!

As part of this effort, the occasional two spaces that
preceeded the Mapping column have been reduced to one.

Reference(s):
       new usage & fix coding style
commit d50884788d
       improve translations
commit 0022b6ec5d
       add gettext support
commit d59cf08c9d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2013-03-18 18:42:07 +01:00
Jim Warner
3262143a29 pmap: trade inept width approach for printf's built-in
When the new -X/-XX options were introduced, the width
and justification requirements were satisfied with a 2
step approach. First, format strings would dynamically
be built and then employed in the subsequent printf().

This was a total waste of time and resources since the
printf family of functions provide for the '*' width &
precision conventions to meet this need via arguments.

But even more importantly that foolish endeavor always
hides the potential warnings like the one shown below.

Henceforth this pmap will printf in the proper manner!

Reference(s):
       new options for pmap
commit faec340719
pmap.c:459:4: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat]

Signed-off-by: Jim Warner <james.warner@comcast.net>
2013-03-18 18:42:07 +01:00
Jim Warner
9db537503d pmap: formatting only changes, for minimum consistency
This patch just adjusts miscellaneous indentation etc.
so we can begin the odious task of addressing the pmap
nls needs with at least a consistently formatted base.

Signed-off-by: Jim Warner <james.warner@comcast.net>
2013-03-18 18:42:07 +01:00
Jim Warner
f85439e42d pmap: restore a proper response when arguments missing
Ever since pmap was refactored via the reference below
(and sprinkled with those damn tabs), the response for
the absence of any argument has been an error message.

This patch restores the proper behavior ('usage' text)
and updates the dejagnu 'no arguments' expect pattern.

Reference(s):
commit d50884788d

Signed-off-by: Jim Warner <james.warner@comcast.net>
2013-03-18 18:42:07 +01:00