1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
/******************************************************************************/
/* THE BEER-WARE LICENSE (Revision 42): */
/* As long as you retain this notice you can do whatever you want with this */
/* stuff. If we meet some day, and you think this stuff is worth it, */
/* you can buy me a beer in return. Ned Ludd. --solarx */
/******************************************************************************/
/*
* normal compile.
* cc -o pspax pspax.c
* or with libcap.
* cc -o pspax pspax.c -DWANT_SYSCAP -lcap
*/
static const char rcsid[] = "$Id: pspax.c,v 1.51 2012/11/18 07:39:45 vapier Exp $";
const char argv0[] = "pspax";
#include "paxinc.h"
#include <grp.h>
#ifdef WANT_SYSCAP
# undef _POSIX_SOURCE
# include <sys/capability.h>
# define WRAP_SYSCAP(x) x
#else
# define WRAP_SYSCAP(x)
#endif
#define PROC_DIR "/proc"
/* variables to control behavior */
static char show_all = 0;
static char verbose = 0;
static char show_banner = 1;
static char show_phdr = 0;
static char show_addr = 0;
static char noexec = 1;
static char writeexec = 1;
static char wide_output = 0;
static pid_t show_pid = 0;
static uid_t show_uid = -1;
static gid_t show_gid = -1;
static FILE *proc_fopen(pid_t pid, const char *file)
{
char path[__PAX_UTILS_PATH_MAX];
snprintf(path, sizeof(path), PROC_DIR "/%u/%s", pid, file);
path[sizeof(path) - 1] = '\0';
return fopen(path, "r");
}
static char *get_proc_name_cmdline(pid_t pid)
{
FILE *fp;
static char str[1024];
fp = proc_fopen(pid, "cmdline");
if (fp == NULL)
return NULL;
if (fscanf(fp, "%s.1023", str) != 1) {
fclose(fp);
return NULL;
}
return (str);
}
static char *get_proc_name(pid_t pid)
{
FILE *fp;
static char str[BUFSIZ];
if (wide_output)
return get_proc_name_cmdline(pid);
fp = proc_fopen(pid, "stat");
if (fp == NULL)
return NULL;
if (fscanf(fp, "%*d %s.16", str) != 1) {
fclose(fp);
return NULL;
}
if (*str) {
str[strlen(str) - 1] = '\0';
str[16] = 0;
}
fclose(fp);
return (str+1);
}
static int get_proc_maps(pid_t pid)
{
FILE *fp;
static char str[BUFSIZ];
if ((fp = proc_fopen(pid, "maps")) == NULL)
return -1;
while (fgets(str, sizeof(str), fp)) {
char *p;
if ((p = strchr(str, ' ')) != NULL) {
if (strlen(p) < 6)
continue;
/* 0x0-0x0 rwxp fffff000 00:00 0 */
/* 0x0-0x0 R+W+XP fffff000 00:00 0 */
++p; /* ' ' */
++p; /* r */
if (*p == '+')
++p;
/* FIXME: all of wx, w+, +x, ++ indicate w|x */
if (tolower(*p) == 'w') {
++p;
if (*p == '+')
++p;
if (tolower(*p) == 'x') {
fclose(fp);
return 1;
}
}
}
}
fclose(fp);
return 0;
}
static int print_executable_mappings(pid_t pid)
{
FILE *fp;
static char str[BUFSIZ];
if ((fp = proc_fopen(pid, "maps")) == NULL)
return -1;
while (fgets(str, sizeof(str), fp)) {
char *p;
if ((p = strchr(str, ' ')) != NULL) {
if (strlen(p) < 6)
continue;
/* 0x0-0x0 rwxp fffff000 00:00 0 */
/* 0x0-0x0 R+W+XP fffff000 00:00 0 */
++p; /* ' ' */
++p; /* r */
if (*p == '+')
++p;
/* FIXME: all of wx, w+, +x, ++ indicate w|x */
if (tolower(*p) == 'w') {
++p;
if (*p == '+')
++p;
if (tolower(*p) == 'x')
printf(" %s", str);
}
}
}
fclose(fp);
return 0;
}
#ifdef __BOUNDS_CHECKING_ON
# define NOTE_TO_SELF warn( \
"This is bullshit but getpwuid() is leaking memory and I wasted a few hrs 1 day tracking it down in pspax\n" \
"Later on I forgot I tracked it down before and saw pspax leaking memory so I tracked it down all over again (silly me)\n" \
"Hopefully the getpwuid()/nis/nss/pam or whatever wont suck later on in the future.")
#else
# define NOTE_TO_SELF
#endif
static struct passwd *get_proc_passwd(pid_t pid)
{
struct stat st;
struct passwd *pwd;
char path[__PAX_UTILS_PATH_MAX];
snprintf(path, sizeof(path), PROC_DIR "/%u/stat", pid);
if (stat(path, &st) != -1)
if ((pwd = getpwuid(st.st_uid)) != NULL)
return pwd;
return NULL;
}
static char *get_proc_status(pid_t pid, const char *name)
{
FILE *fp;
size_t len;
static char str[BUFSIZ];
if ((fp = proc_fopen(pid, "status")) == NULL)
return NULL;
len = strlen(name);
while (fgets(str, sizeof(str), fp)) {
if (strncasecmp(str, name, len) != 0)
continue;
if (str[len] == ':') {
fclose(fp);
str[strlen(str) - 1] = 0;
return (str + len + 2);
}
}
fclose(fp);
return NULL;
}
static char *get_pid_attr(pid_t pid)
{
FILE *fp;
char *p;
static char buf[BUFSIZ];
if ((fp = proc_fopen(pid, "attr/current")) == NULL)
return NULL;
if (fgets(buf, sizeof(buf), fp) != NULL)
if ((p = strchr(buf, '\n')) != NULL)
*p = 0;
fclose(fp);
return buf;
}
static char *get_pid_addr(pid_t pid)
{
FILE *fp;
char *p;
static char buf[BUFSIZ];
if ((fp = proc_fopen(pid, "ipaddr")) == NULL)
return NULL;
if (fgets(buf, sizeof(buf), fp) != NULL)
if ((p = strchr(buf, '\n')) != NULL)
*p = 0;
fclose(fp);
return buf;
}
static const char *get_proc_type(pid_t pid)
{
char fname[32];
elfobj *elf;
const char *ret;
snprintf(fname, sizeof(fname), PROC_DIR "/%u/exe", pid);
if ((elf = readelf(fname)) == NULL)
return NULL;
ret = get_elfetype(elf);
unreadelf(elf);
return ret;
}
static char *scanelf_file_phdr(elfobj *elf)
{
static char ret[8];
unsigned long i, off, multi_stack, multi_load;
memcpy(ret, "--- ---\0", 8);
multi_stack = multi_load = 0;
if (elf->phdr) {
uint32_t flags;
#define SHOW_PHDR(B) \
if (elf->elf_class == ELFCLASS ## B) { \
Elf ## B ## _Ehdr *ehdr = EHDR ## B (elf->ehdr); \
Elf ## B ## _Phdr *phdr = PHDR ## B (elf->phdr); \
for (i = 0; i < EGET(ehdr->e_phnum); i++) { \
if (EGET(phdr[i].p_type) == PT_GNU_STACK) { \
if (multi_stack++) warnf("%s: multiple PT_GNU_STACK's !?", elf->filename); \
off = 0; \
} else if (EGET(phdr[i].p_type) == PT_LOAD) { \
off = 4; \
} else \
continue; \
flags = EGET(phdr[i].p_flags); \
memcpy(ret+off, gnu_short_stack_flags(flags), 3); \
} \
}
SHOW_PHDR(32)
SHOW_PHDR(64)
}
return ret;
}
/* we scan the elf file two times when the -e flag is given. But we don't need -e very often so big deal */
static const char *get_proc_phdr(pid_t pid)
{
char fname[32];
elfobj *elf;
const char *ret;
snprintf(fname, sizeof(fname), PROC_DIR "/%u/exe", pid);
if ((elf = readelf(fname)) == NULL)
return NULL;
ret = scanelf_file_phdr(elf);
unreadelf(elf);
return ret;
}
static void pspax(const char *find_name)
{
register DIR *dir;
register struct dirent *de;
pid_t pid;
pid_t ppid = show_pid;
int have_attr, have_addr, wx;
struct passwd *pwd;
struct stat st;
const char *pax, *type, *name, *attr, *addr;
char *caps;
WRAP_SYSCAP(ssize_t length; cap_t cap_d;)
WRAP_SYSCAP(cap_d = cap_init());
dir = opendir(PROC_DIR);
if (dir == NULL || chdir(PROC_DIR))
errp(PROC_DIR);
if (access("/proc/self/attr/current", R_OK) != -1)
have_attr = 1;
else
have_attr = 0;
if ((access("/proc/self/ipaddr", R_OK) != -1) && show_addr)
have_addr = 1;
else
have_addr = 0;
if (show_banner)
printf("%-8s %-6s %-6s %-4s %-10s %-16s %-4s %-4s %s %s\n",
"USER", "PID", "PAX", "MAPS", "ETYPE", "NAME", "CAPS", have_attr ? "ATTR" : "",
have_addr ? "IPADDR" : "", show_phdr ? "STACK LOAD" : "");
while ((de = readdir(dir))) {
errno = 0;
stat(de->d_name, &st);
if ((errno != ENOENT) && (errno != EACCES)) {
pid = (pid_t) atoi((char *) basename((char *) de->d_name));
if (find_name && pid) {
char *str = get_proc_name(pid);
if (!str)
continue;
if (strcmp(str, find_name) != 0)
pid = 0;
}
if (((ppid > 0) && (pid != ppid)) || !pid)
continue;
wx = get_proc_maps(pid);
if (noexec != writeexec) {
if ((wx == 1) && (writeexec != wx))
goto next_pid;
if ((wx == 0) && writeexec)
goto next_pid;
}
pwd = get_proc_passwd(pid);
pax = get_proc_status(pid, "PAX");
type = get_proc_type(pid);
name = get_proc_name(pid);
attr = (have_attr ? get_pid_attr(pid) : NULL);
addr = (have_addr ? get_pid_addr(pid) : NULL);
if (show_uid != -1 && pwd)
if (pwd->pw_uid != show_uid)
continue;
if (show_gid != -1 && pwd)
if (pwd->pw_gid != show_gid)
continue;
/* this is a non-POSIX function */
caps = NULL;
WRAP_SYSCAP(capgetp(pid, cap_d));
WRAP_SYSCAP(caps = cap_to_text(cap_d, &length));
if (pwd && strlen(pwd->pw_name) >= 8)
pwd->pw_name[8] = 0;
if (show_all || type) {
printf("%-8s %-6d %-6s %-4s %-10s %-16s %-4s %s %s %s\n",
pwd ? pwd->pw_name : "--------",
pid,
pax ? pax : "---",
(wx == 1) ? "w|x" : (wx == -1) ? "---" : "w^x",
type ? type : "-------",
name ? name : "-----",
caps ? caps : " = ",
attr ? attr : "",
addr ? addr : "",
show_phdr ? get_proc_phdr(pid) : "");
if (verbose && wx)
print_executable_mappings(pid);
}
WRAP_SYSCAP(if (caps) cap_free(caps));
next_pid:
continue;
}
}
closedir(dir);
}
/* usage / invocation handling functions */
#define PARSE_FLAGS "aeip:u:g:nwWvCBhV"
#define a_argument required_argument
static struct option const long_opts[] = {
{"all", no_argument, NULL, 'a'},
{"header", no_argument, NULL, 'e'},
{"ipaddr", no_argument, NULL, 'i'},
{"pid", a_argument, NULL, 'p'},
{"user", a_argument, NULL, 'u'},
{"group", a_argument, NULL, 'g'},
{"nx", no_argument, NULL, 'n'},
{"wx", no_argument, NULL, 'w'},
{"wide", no_argument, NULL, 'W'},
{"verbose", no_argument, NULL, 'v'},
{"nocolor", no_argument, NULL, 'C'},
{"nobanner", no_argument, NULL, 'B'},
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, no_argument, NULL, 0x0}
};
static const char * const opts_help[] = {
"Show all processes",
"Print GNU_STACK/PT_LOAD markings",
"Print ipaddr info if supported",
"Process ID/pid #",
"Process user/uid #",
"Process group/gid #",
"Only display w^x processes",
"Only display w|x processes",
"Wide output display of cmdline",
"Be verbose about executable mappings",
"Don't emit color in output",
"Don't display the header",
"Print this help and exit",
"Print version and exit",
NULL
};
/* display usage and exit */
static void usage(int status)
{
int i;
printf("* List ELF/PaX information about running processes\n\n"
"Usage: %s [options]\n\n", argv0);
fputs("Options:\n", stdout);
for (i = 0; long_opts[i].name; ++i)
printf(" -%c, --%-12s* %s\n", long_opts[i].val,
long_opts[i].name, opts_help[i]);
#ifdef MANLYPAGE
for (i = 0; long_opts[i].name; ++i)
printf(".TP\n\\fB\\-%c, \\-\\-%s\\fR\n%s\n", long_opts[i].val,
long_opts[i].name, opts_help[i]);
#endif
exit(status);
}
/* parse command line arguments and preform needed actions */
static void parseargs(int argc, char *argv[])
{
int flag;
struct passwd *pwd = NULL;
struct group *gwd = NULL;
opterr = 0;
while ((flag=getopt_long(argc, argv, PARSE_FLAGS, long_opts, NULL)) != -1) {
switch (flag) {
case 'V': /* version info */
printf("pax-utils-%s: %s compiled %s\n%s\n"
"%s written for Gentoo by <solar and vapier @ gentoo.org>\n",
VERSION, __FILE__, __DATE__, rcsid, argv0);
exit(EXIT_SUCCESS);
break;
case 'h': usage(EXIT_SUCCESS); break;
case 'C': color_init(true); break;
case 'B': show_banner = 0; break;
case 'a': show_all = 1; break;
case 'e': show_phdr = 1; break;
case 'i': show_addr = 1; break;
case 'p': show_pid = atoi(optarg); break;
case 'n': noexec = 1; writeexec = 0; break;
case 'w': noexec = 0; writeexec = 1; break;
case 'W': wide_output = 1; break;
case 'v': verbose++; break;
case 'u':
show_uid = atoi(optarg);
if (show_uid == 0 && (strcmp(optarg, "0") != 0)) {
pwd = getpwnam(optarg);
if (pwd)
show_uid = pwd->pw_uid;
else
err("unknown uid");
}
break;
case 'g':
show_gid = atoi(optarg);
if (show_gid == 0 && (strcmp(optarg, "0") != 0)) {
gwd = getgrnam(optarg);
if (gwd)
show_gid = gwd->gr_gid;
else
err("unknown gid");
}
break;
case ':':
case '?':
warn("Unknown option or missing parameter");
usage(EXIT_FAILURE);
break;
default:
err("Unhandled option '%c'", flag);
break;
}
}
}
int main(int argc, char *argv[])
{
char *name = NULL;
color_init(false);
parseargs(argc, argv);
if ((optind < argc) && (show_pid == 0))
name = argv[optind];
pspax(name);
NOTE_TO_SELF;
return EXIT_SUCCESS;
}
|