File: find.c

package info (click to toggle)
binfmt-support 2.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,044 kB
  • ctags: 2,354
  • sloc: ansic: 23,490; sh: 5,137; python: 159; makefile: 131
file content (238 lines) | stat: -rw-r--r-- 6,949 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
/* query.c - find interpreter for a given executable
 *
 * Copyright (c) 2002, 2010, 2011 Colin Watson <cjwatson@debian.org>.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <pipeline.h>

#include "gl_xlist.h"
#include "gl_array_list.h"
#include "xalloc.h"
#include "xvasprintf.h"

#include "error.h"
#include "format.h"
#include "paths.h"

static size_t expand_hex (char **str)
{
    size_t len;
    char *new, *p, *s;

    /* The returned string is always no longer than the original. */
    len = strlen (*str);
    p = new = xmalloc (len + 1);
    s = *str;
    while (*s) {
	if (s <= *str + len - 4 && s[0] == '\\' && s[1] == 'x') {
	    char in[3];
	    char *end;
	    long hex;

	    in[0] = s[2];
	    in[1] = s[3];
	    in[2] = '\0';
	    hex = strtol (in, &end, 16);
	    if (end == in + 2) {
		*p++ = (char) hex;
		s += 4;
		continue;
	    }
	}
	*p++ = *s++;
    }
    *p = 0;

    free (*str);
    *str = new;
    return p - new;
}

static void load_all_formats (gl_list_t *formats)
{
    DIR *dir;
    struct dirent *entry;

    dir = opendir (admindir);
    if (!dir)
	quit_err ("unable to open %s", admindir);
    *formats = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
    while ((entry = readdir (dir)) != NULL) {
	char *admindir_name, *procdir_name;
	struct stat st;
	struct binfmt *binfmt;
	size_t mask_size;

	if (!strcmp (entry->d_name, ".") || !strcmp (entry->d_name, ".."))
	    continue;
	procdir_name = xasprintf ("%s/%s", procdir, entry->d_name);
	if (stat (procdir_name, &st) == -1) {
	    free (procdir_name);
	    continue;
	}
	free (procdir_name);
	admindir_name = xasprintf ("%s/%s", admindir, entry->d_name);
	binfmt = binfmt_load (entry->d_name, admindir_name, 0);
	free (admindir_name);

	/* binfmt_load should always make these at least empty strings,
	 * never null pointers.
	 */
	assert (binfmt->type);
	assert (binfmt->magic);
	assert (binfmt->mask);
	assert (binfmt->offset);
	assert (binfmt->interpreter);
	assert (binfmt->detector);

	binfmt->magic_size = expand_hex (&binfmt->magic);
	mask_size = expand_hex (&binfmt->mask);
	if (mask_size && mask_size != binfmt->magic_size) {
	    /* A warning here would be inappropriate, as it would often be
	     * emitted for unrelated programs.
	     */
	    binfmt_free (binfmt);
	    continue;
	}
	if (!*binfmt->offset)
	    binfmt->offset = xstrdup ("0");
	gl_list_add_last (*formats, binfmt);
    }
    closedir (dir);
}

gl_list_t find_interpreters (const char *path)
{
    gl_list_t formats, ok_formats, interpreters;
    gl_list_iterator_t format_iter;
    const struct binfmt *binfmt;
    size_t toread;
    char *buf;
    FILE *target_file;
    const char *dot, *extension = NULL;

    load_all_formats (&formats);

    /* Find out how much of the file we need to read.  The kernel doesn't
     * currently let this be more than 128, so we shouldn't need to worry
     * about huge memory consumption.
     */
    toread = 0;
    format_iter = gl_list_iterator (formats);
    while (gl_list_iterator_next (&format_iter, (const void **) &binfmt,
				  NULL)) {
	if (!strcmp (binfmt->type, "magic")) {
	    size_t size;

	    size = atoi (binfmt->offset) + binfmt->magic_size;
	    if (size > toread)
		toread = size;
	}
    }
    gl_list_iterator_free (&format_iter);

    buf = xzalloc (toread);
    target_file = fopen (path, "r");
    if (!target_file)
	quit_err ("unable to open %s", path);
    if (fread (buf, 1, toread, target_file) == 0)
	/* Ignore errors; the buffer is zero-filled so attempts to match
	 * beyond the data read here will fail anyway.
	 */
	;
    fclose (target_file);

    /* Now the horrible bit.  Since there isn't a real way to plug userspace
     * detectors into the kernel (which is why this program exists in the
     * first place), we have to redo the kernel's work.  Luckily it's a
     * fairly simple job ... see linux/fs/binfmt_misc.c:check_file().
     *
     * There is a small race between the kernel performing this check and us
     * performing it.  I don't believe that this is a big deal; certainly
     * there can be no privilege elevation involved unless somebody
     * deliberately makes a set-id binary a binfmt handler, in which case
     * "don't do that, then".
     */
    dot = strrchr (path, '.');
    if (dot)
	extension = dot + 1;

    ok_formats = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL, true);
    format_iter = gl_list_iterator (formats);
    while (gl_list_iterator_next (&format_iter, (const void **) &binfmt,
				  NULL)) {
	if (!strcmp (binfmt->type, "magic")) {
	    char *segment;

	    segment = xmalloc (binfmt->magic_size);
	    memcpy (segment, buf + atoi (binfmt->offset), binfmt->magic_size);
	    if (*binfmt->mask)
		for (size_t i = 0; i < toread && i < binfmt->magic_size; ++i)
		    segment[i] &= binfmt->mask[i];
	    if (!memcmp (segment, binfmt->magic, binfmt->magic_size))
		gl_list_add_last (ok_formats, binfmt);
	    free (segment);
	} else {
	    if (extension && !strcmp (extension, binfmt->magic))
		gl_list_add_last (ok_formats, binfmt);
	}
    }
    gl_list_iterator_free (&format_iter);

    /* Everything in ok_formats is now a candidate.  Loop through twice,
     * once to try everything with a detector and once to try everything
     * without.
     */
    interpreters = gl_list_create_empty (GL_ARRAY_LIST, NULL, NULL, NULL,
					 true);
    format_iter = gl_list_iterator (ok_formats);
    while (gl_list_iterator_next (&format_iter, (const void **) &binfmt,
				  NULL)) {
	if (*binfmt->detector) {
	    pipeline *detector;

	    detector = pipeline_new_command_args (binfmt->detector,
						  path, NULL);
	    if (pipeline_run (detector) == 0)
		gl_list_add_last (interpreters, binfmt);
	}
    }
    gl_list_iterator_free (&format_iter);

    format_iter = gl_list_iterator (ok_formats);
    while (gl_list_iterator_next (&format_iter, (const void **) &binfmt,
				  NULL)) {
	if (!*binfmt->detector)
	    gl_list_add_last (interpreters, binfmt);
    }
    gl_list_iterator_free (&format_iter);

    return interpreters;
}