forked from lastpass/lastpass-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.c
91 lines (78 loc) · 2.02 KB
/
process.c
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
/*
* Copyright (c) 2014 LastPass.
*
*
*/
#include "process.h"
#include "util.h"
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <limits.h>
#if defined(__linux__)
#include <sys/prctl.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <libproc.h>
#include <sys/ptrace.h>
#endif
void process_set_name(const char *name)
{
#if defined(__linux__)
prctl(PR_SET_NAME, name);
#endif
if (!ARGC || !ARGV)
return;
for (int i = 0; i < ARGC; ++i) {
for (char *p = ARGV[i]; *p; ++p)
*p = '\0';
}
strcpy(ARGV[0], name);
}
#if defined(__linux__) || defined(__CYGWIN__)
#define DEVPROC_NAME "exe"
#define DEVPROC_SELF "self"
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#define DEVPROC_NAME "file"
#define DEVPROC_SELF "curproc"
#elif defined(__NetBSD__)
#define DEVPROC_NAME "exe"
#define DEVPROC_SELF "curproc"
#endif
#if defined(DEVPROC_NAME)
bool process_is_same_executable(pid_t pid)
{
_cleanup_free_ char *proc = NULL;
char resolved_them[PATH_MAX + 1] = { 0 }, resolved_me[PATH_MAX + 1] = { 0 };
xasprintf(&proc, "/proc/%lu/" DEVPROC_NAME, (unsigned long)pid);
if (readlink(proc, resolved_them, PATH_MAX) < 0 || readlink("/proc/" DEVPROC_SELF "/" DEVPROC_NAME, resolved_me, PATH_MAX) < 0)
return false;
if (strcmp(resolved_them, resolved_me))
return false;
return true;
}
void process_disable_ptrace(void)
{
#if defined(__linux__)
prctl(PR_SET_DUMPABLE, 0);
#endif
struct rlimit limit = { 0, 0 };
setrlimit(RLIMIT_CORE, &limit);
}
#elif defined(__APPLE__) && defined(__MACH__)
bool process_is_same_executable(pid_t pid)
{
char resolved_them[PROC_PIDPATHINFO_MAXSIZE], resolved_me[PROC_PIDPATHINFO_MAXSIZE];
if (proc_pidpath(pid, resolved_them, sizeof(resolved_them)) <= 0 || proc_pidpath(getpid(), resolved_me, sizeof(resolved_me)) <= 0)
return false;
if (strcmp(resolved_them, resolved_me))
return false;
return true;
}
void process_disable_ptrace(void)
{
ptrace(PT_DENY_ATTACH, 0, 0, 0);
struct rlimit limit = { 0, 0 };
setrlimit(RLIMIT_CORE, &limit);
}
#endif