File: proc.c

package info (click to toggle)
nufw 2.4.3-2.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,728 kB
  • sloc: ansic: 32,174; sh: 10,146; python: 4,176; makefile: 965; perl: 630; lex: 176; php: 168; yacc: 117; xml: 34
file content (371 lines) | stat: -rw-r--r-- 8,312 bytes parent folder | download | duplicates (3)
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
/*
 ** Copyright 2005-2007 - INL
 ** Written by Eric Leblond <regit@inl.fr>
 **            Victor Stinner <haypo@inl.fr>
 ** INL http://www.inl.fr/
 **
 ** $Id$
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation, version 3 of the License.
 **
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 **
 ** You should have received a copy of the GNU General Public License
 ** along with this program; if not, write to the Free Software
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include <config.h>

#include "libnuclient.h"
#include "proc.h"
#include <sasl/saslutil.h>

#ifdef LINUX

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <netdb.h>
#include <paths.h>
#include <pwd.h>
#include <getopt.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <dirent.h>

#include "security.h"

#include <nubase.h>
#include <nussl_hash.h>

#define BLOCKSIZE 64


/**
 * \addtogroup libnuclient
 * @{
 */

static struct prg_node {
	struct prg_node *next; /** Pointer to next element in the single chained list */
	unsigned long inode;   /** Inode of the program executable binary */
	char name[PROGNAME_WIDTH];
			       /** Name of the program (encoded in UTF-8) */
	char sig[4 * NUSSL_HASH_MAX_SIZE + 48]; /* HASH size + prefix */
} *prg_hash[PRG_HASH_SIZE];

#define PROGNAME_WIDTHs PROGNAME_WIDTH1(PROGNAME_WIDTH)
#define PROGNAME_WIDTH1(s) PROGNAME_WIDTH2(s)
#define PROGNAME_WIDTH2(s) #s

#define PRG_HASHIT(x) ((x) % PRG_HASH_SIZE)

#define PRG_LOCAL_ADDRESS "local_address"
#define PRG_INODE	 "inode"
#define PRG_SOCKET_PFX    "socket:["
#define PRG_SOCKET_PFXl (strlen(PRG_SOCKET_PFX))
#define PRG_SOCKET_PFX2   "[0000]:"
#define PRG_SOCKET_PFX2l  (strlen(PRG_SOCKET_PFX2))

#ifndef PATH_MAX
#  define PATH_MAX 4096
#endif

static void prg_cache_add(unsigned long inode, char *name)
{
	unsigned hi = PRG_HASHIT(inode);
	struct prg_node **pnp, *pn;

	prg_cache_loaded = 2;
	for (pnp = prg_hash + hi; (pn = *pnp); pnp = &pn->next) {
		if (pn->inode == inode) {
			/* Some warning should be appropriate here
			   as we got multiple processes for one i-node */
			return;
		}
	}
	*pnp = malloc(sizeof(**pnp));
	if (*pnp == NULL)
		return;
	pn = *pnp;
	pn->next = NULL;
	pn->inode = inode;
	SECURE_STRNCPY(pn->name, name, sizeof(pn->name));
	pn->sig[0] = 0;
}

const char *prg_cache_get(unsigned long inode)
{
	unsigned hi = PRG_HASHIT(inode);
	struct prg_node *pn;

	for (pn = prg_hash[hi]; pn; pn = pn->next)
		if (pn->inode == inode)
			return (pn->name);
	return ("-");
}

const char *prg_cache_getsig(int algo, unsigned long inode)
{
	unsigned hi = PRG_HASHIT(inode);
	struct prg_node *pn;
	size_t size;
	unsigned char pnsig[4 * NUSSL_HASH_MAX_SIZE];
	int ret;

#define SHA512_PREFIX "{SHA512}"

	for (pn = prg_hash[hi]; pn; pn = pn->next) {
		if (pn->inode == inode) {
			if (pn->sig[0] == 0) {
				char * hexnum;
				ret = nussl_hash_file(algo, pn->name,
						pnsig, &size);
				if (ret == 0) {
					hexnum = pn->sig + strlen(SHA512_PREFIX);
					memcpy(pn->sig, SHA512_PREFIX, strlen(SHA512_PREFIX));
					bin2hex(size, pnsig, hexnum);
				} else {
					pn->sig[0] = '\0';
				}
			}
			return (pn->sig);
		}
	}
	return "-";
}

void prg_cache_clear(void)
{
	struct prg_node **pnp;

	if (prg_cache_loaded == 2) {
		for (pnp = prg_hash; pnp < prg_hash + PRG_HASH_SIZE; pnp++) {
			struct prg_node *it = *pnp;
			struct prg_node *node;
			while (it != NULL) {
				node = it;
				if (node == NULL) break;
				it = node->next;
				free(node);
			}
			*pnp = NULL;
		}
	}
	prg_cache_loaded = 0;
}

static int extract_type_1_socket_inode(char lname[],
				       unsigned long *inode_p)
{
	char *inode_str;
	char *serr;
	size_t len = strlen(lname);

	/* If lname is of the form "socket:[12345]", extract the "12345"
	   as *inode_p.  Otherwise, return -1 as *inode_p.
	 */
	if (len < PRG_SOCKET_PFXl + 3)
		return (-1);
	if (memcmp(lname, PRG_SOCKET_PFX, PRG_SOCKET_PFXl))
		return (-1);
	if (lname[len - 1] != ']')
		return (-1);

	inode_str = lname + PRG_SOCKET_PFXl;
	lname[len - 1] = '\0';
	*inode_p = strtol(inode_str, &serr, 0);
	if (serr == NULL || *serr != '\0' || *inode_p >= INT_MAX) {
		lname[len - 1] = ']';
		printf("no %s\n", lname);
		return (-1);
	}
	lname[len - 1] = ']';
	return (0);
}

static int extract_type_2_socket_inode(const char lname[],
				       unsigned long *inode_p)
{

	char *serr;

	/* If lname is of the form "[0000]:12345", extract the "12345"
	   as *inode_p.  Otherwise, return -1 as *inode_p.
	 */

	if (strlen(lname) < PRG_SOCKET_PFX2l + 1)
		return (-1);
	if (memcmp(lname, PRG_SOCKET_PFX2, PRG_SOCKET_PFX2l))
		return (-1);

	*inode_p = strtol(lname + PRG_SOCKET_PFX2l, &serr, 0);
	if (serr == NULL || *serr != '\0' || *inode_p >= INT_MAX)
		return (-1);
	return (0);
}

/**
 * Check if a string contains an integer
 *
 * \return 1 if it's a number, 0 otherwise
 */
int str_is_integer(const char *str)
{
	for (; *str != '\0'; ++str) {
		if (!isdigit(*str))
			return 0;
	}
	return 1;
}

/**
 * Secure version of readlink()
 *
 * \return 0 if an error occurs, 1 if ok
 */
int secure_readlink(const char *filename, char *buffer,
		    unsigned int buflen)
{
	int ret;

	/* call readlink (add 'canary' to check "buffer overflow") */
	buffer[buflen - 1] = '\0';
	ret = readlink(filename, buffer, buflen);

	/* error if readlink fails */
	if (ret < 0)
		return 0;

	/* error if buffer is too small ("buffer overflow") */
	if (buffer[buflen - 1] != '\0')
		return 0;

	/* that should never happens, but ... */
	if (((int) buflen - 1) < ret)
		return 0;

	/* write nul byte at the end */
	buffer[ret] = '\0';
	return 1;
}

/**
 * Walk in directoty like "/proc/123/fd/"
 */
void prg_cache_load_sub(DIR * dir, const char *path_process,
			const char *path_fd)
{
	char path[PATH_MAX];
	char lname[30];
	char finbuf[PROGNAME_WIDTH];
	unsigned long inode;
	struct dirent *file;

	while ((file = readdir(dir)) != NULL) {
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
		if (file->d_type != DT_LNK)
			continue;
#endif

		/* read link of "/proc/123/fd/FILENAME" */
		if (!secure_snprintf
		    (path, sizeof(path), "%s/%s", path_fd, file->d_name))
			continue;
		if (!secure_readlink(path, lname, sizeof(lname)))
			continue;

		/*
		 * extract inode number from name like "socket:[12345]"
		 * or "[0000]:12345"
		 */
		if (extract_type_1_socket_inode(lname, &inode) < 0)
			if (extract_type_2_socket_inode(lname, &inode) < 0)
				continue;

		/* get exec fullpath */
		if (!secure_snprintf
		    (path, sizeof(path), "%s/exe", path_process))
			continue;
		if (!secure_readlink(path, finbuf, sizeof(finbuf)))
			continue;

		/* add item to the cache */
		prg_cache_add(inode, finbuf);
	}
}


void prg_cache_init()
{
	memset(prg_hash, 0, sizeof(prg_hash));
}

/**
 * Load program cache
 */
void prg_cache_load()
{
	char path_process[PATH_MAX];
	char path_fd[PATH_MAX];
	DIR *dirproc = NULL;
	DIR *dirfd = NULL;
	struct dirent *file;

	if (prg_cache_loaded)
		return;
	prg_cache_loaded = 1;

	/* open directory "/proc" */
	dirproc = opendir("/proc");
	if (dirproc == NULL)
		panic("Fail to open /proc directory!");

	while ((file = readdir(dirproc)) != NULL) {
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
		if (file->d_type != DT_DIR)
			continue;
#endif
		if (!str_is_integer(file->d_name))
			continue;

		/* create path like "/proc/123" */
		if (!secure_snprintf
		    (path_process, sizeof(path_process), "/proc/%s",
		     file->d_name))
			continue;

		/* create path like "/proc/123/fd" */
		if (!secure_snprintf
		    (path_fd, sizeof(path_fd), "%s/fd", path_process))
			continue;

		/* open directory like "/proc/123/fd" */
		dirfd = opendir(path_fd);
		if (dirfd != NULL) {
			prg_cache_load_sub(dirfd, path_process, path_fd);
			closedir(dirfd);
		}
	}
	closedir(dirproc);
}

/** @} */

#endif				/* of #ifdef LINUX */