source: trunk/src/sh_prelink.c@ 115

Last change on this file since 115 was 107, checked in by rainer, 17 years ago

Fix incorrect handling of files with zero size in GrowingLogFiles.

File size: 7.4 KB
Line 
1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2004 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#include <string.h>
23#include <sys/types.h>
24#include <signal.h>
25
26#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
27
28#include "samhain.h"
29#include "sh_tiger.h"
30#include "sh_extern.h"
31#include "sh_utils.h"
32#include "sh_unix.h"
33
34#undef FIL__
35#define FIL__ _("sh_prelink.c")
36
37static char * prelink_path = NULL;
38static char * prelink_hash = NULL;
39
40int sh_prelink_set_path (const char * str)
41{
42 SL_ENTER(_("sh_prelink_set_path"));
43 if (prelink_path != NULL)
44 SH_FREE(prelink_path);
45 if (str[0] != '/')
46 {
47 prelink_path = NULL;
48 SL_RETURN((-1), _("sh_prelink_set_path"));
49 }
50
51 prelink_path = sh_util_strdup(str);
52
53 SL_RETURN(0, _("sh_prelink_set_path"));
54}
55
56int sh_prelink_set_hash (const char * str)
57{
58 size_t len;
59 SL_ENTER(_("sh_prelink_set_hash"));
60 if (prelink_hash != NULL)
61 SH_FREE(prelink_hash);
62 len = sl_strlen (str);
63 if (len != KEY_LEN)
64 {
65 prelink_hash = NULL;
66 SL_RETURN((-1), _("sh_prelink_set_hash"));
67 }
68 prelink_hash = SH_ALLOC(len+1);
69 (void) sl_strlcpy(prelink_hash, str, len+1);
70 SL_RETURN(0, _("sh_prelink_set_hash"));
71}
72
73int sh_prelink_iself (SL_TICKET fd, off_t size, int alert_timeout, char * path)
74{
75 long status;
76 char magic[4];
77 char * tmp;
78
79 /* 42 bytes is about the minimum an ELF binary might have
80 * (with plenty of hacks to reduce the size, such as interleaving
81 * the code with the header...)
82 */
83 if (size < 42)
84 return S_FALSE;
85
86 status = sl_read_timeout (fd, magic, 4, alert_timeout);
87 (void) sl_rewind(fd);
88 if (status == 4)
89 {
90 /*@-usedef@*/
91 if (magic[0] == (char) 0x7f &&
92 magic[1] == 'E' &&
93 magic[2] == 'L' &&
94 magic[3] == 'F')
95 return S_TRUE;
96 /*@+usedef@*/
97 }
98 else
99 {
100 tmp = sh_util_safe_name (path);
101 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, status, MSG_E_SUBGPATH,
102 _("Error reading file"), _("sh_prelink_iself"), tmp);
103 SH_FREE(path);
104 }
105 return S_FALSE;
106}
107
108extern int get_the_fd (SL_TICKET ticket);
109
110static void sh_prelink_fd(sh_tas_t * task)
111{
112 SL_TICKET ticket;
113 char * tmp;
114
115 if (task->com_ti != (-1))
116 {
117 (void) sl_close(task->com_ti);
118 task->com_fd = -1;
119 task->com_ti = -1;
120 }
121 ticket = sl_open_read(task->command,
122 task->privileged == 0 ? SL_NOPRIV : SL_YESPRIV);
123 if (SL_ISERROR(ticket))
124 {
125 tmp = sh_util_safe_name (task->command);
126 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_READ, tmp);
127 SH_FREE(tmp);
128 return;
129 }
130 tiger_fd = ticket;
131 if (*(task->checksum) == '\0' ||
132 0 == sl_strcmp(task->checksum,
133 sh_tiger_hash (task->command, TIGER_FD, 0)))
134 {
135 task->com_fd = get_the_fd(ticket);
136 task->com_ti = ticket;
137 }
138 else
139 {
140 tmp = sh_util_safe_name (task->command);
141 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, ticket, MSG_E_HASH, tmp);
142 SH_FREE(tmp);
143 (void) sl_close(ticket);
144 }
145 return;
146}
147
148/* returns static storage
149 */
150int sh_prelink_run (char * path, char * file_hash, int alert_timeout)
151{
152 static int init = S_FALSE;
153 static int args_filled = 0;
154 static sh_tas_t task;
155
156 int status = 0;
157 char * p;
158 struct sigaction new_act;
159 struct sigaction old_act;
160
161 SL_ENTER(_("sh_prelink_run"));
162
163 /* reset if path == NULL
164 */
165 if (path == NULL)
166 {
167 if (init == S_FALSE)
168 {
169 SL_RETURN (0, _("sh_prelink_run"));
170 }
171 sh_ext_tas_free(&task);
172 init = S_FALSE;
173 args_filled = 0;
174 SL_RETURN (0, _("sh_prelink_run"));
175 }
176
177 /* initialize task structure
178 */
179 if (init == S_FALSE)
180 {
181 sh_ext_tas_init(&task);
182 p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid);
183 if (p)
184 {
185 (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
186 }
187 (void) sh_ext_tas_add_envv (&task, _("SHELL"),
188 _("/bin/sh"));
189 (void) sh_ext_tas_add_envv (&task, _("PATH"),
190 _("/sbin:/usr/sbin:/bin:/usr/bin"));
191 if (sh.timezone != NULL)
192 {
193 (void) sh_ext_tas_add_envv(&task, "TZ", sh.timezone);
194 }
195 if (prelink_path == NULL)
196 {
197 sh_ext_tas_command(&task, _("/usr/sbin/prelink"));
198 (void) sh_ext_tas_add_argv(&task, _("/usr/sbin/prelink"));
199 }
200 else
201 {
202 sh_ext_tas_command(&task, prelink_path);
203 (void) sh_ext_tas_add_argv(&task, prelink_path);
204 }
205 args_filled = sh_ext_tas_add_argv(&task, _("--verify"));
206
207 if (prelink_hash != NULL)
208 {
209 (void) sl_strlcpy(task.checksum, prelink_hash, KEY_LEN+1);
210 }
211 task.rw = 'r';
212 task.fork_twice = S_FALSE;
213
214 sh_prelink_fd(&task);
215
216 init = S_TRUE;
217 }
218
219 /* rm filename arg if set; fill in filename
220 */
221 if (args_filled == 3)
222 args_filled = sh_ext_tas_rm_argv(&task);
223 if (args_filled == 2)
224 args_filled = sh_ext_tas_add_argv(&task, path);
225 else
226 {
227 sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, args_filled, MSG_E_SUBGEN,
228 _("Bad argument count"), _("sh_prelink_run"));
229 SL_RETURN ((-1), _("sh_prelink_run"));
230 }
231
232 /* open pipe
233 */
234 status = sh_ext_popen(&task);
235 if (status != 0)
236 {
237 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
238 _("Could not open pipe"), _("sh_prelink_run"));
239 SL_RETURN ((-1), _("sh_prelink_run"));
240 }
241
242 if (SL_ISERROR(task.pipeTI))
243 {
244 sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, task.pipeTI, MSG_E_SUBGEN,
245 _("No valid ticket"), _("sh_prelink_run"));
246 SL_RETURN ((-1), _("sh_prelink_run"));
247 }
248
249 /* ignore SIGPIPE (instead get EPIPE if connection is closed)
250 */
251 new_act.sa_handler = SIG_IGN;
252 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
253
254 /* read from pipe
255 */
256 tiger_fd = task.pipeTI;
257
258 sl_read_timeout_prep (task.pipeTI);
259
260 sl_strlcpy(file_hash,
261 sh_tiger_generic_hash (path, TIGER_FD, TIGER_NOLIM, alert_timeout),
262 KEY_LEN+1);
263
264 /* restore old signal handler
265 */
266 (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
267
268 /* close pipe and return exit status
269 */
270 status = sh_ext_pclose(&task);
271 SL_RETURN ((status), _("sh_prelink_run"));
272}
273/* defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
274 */
275#endif
Note: See TracBrowser for help on using the repository browser.