[1] | 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 |
|
---|
| 37 | static char * prelink_path = NULL;
|
---|
| 38 | static char * prelink_hash = NULL;
|
---|
| 39 |
|
---|
[22] | 40 | int sh_prelink_set_path (const char * str)
|
---|
[1] | 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 | }
|
---|
[34] | 50 |
|
---|
| 51 | prelink_path = sh_util_strdup(str);
|
---|
| 52 |
|
---|
[1] | 53 | SL_RETURN(0, _("sh_prelink_set_path"));
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[22] | 56 | int sh_prelink_set_hash (const char * str)
|
---|
[1] | 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 |
|
---|
[102] | 73 | int sh_prelink_iself (SL_TICKET fd, off_t size, int alert_timeout, char * path)
|
---|
[1] | 74 | {
|
---|
[102] | 75 | long status;
|
---|
| 76 | char magic[4];
|
---|
| 77 | char * tmp;
|
---|
[101] | 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 | */
|
---|
[8] | 83 | if (size < 42)
|
---|
| 84 | return S_FALSE;
|
---|
[101] | 85 |
|
---|
[131] | 86 | status = sl_read_timeout (fd, magic, 4, alert_timeout, SL_FALSE);
|
---|
[8] | 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 | }
|
---|
[102] | 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 | }
|
---|
[8] | 105 | return S_FALSE;
|
---|
[1] | 106 | }
|
---|
| 107 |
|
---|
| 108 | extern int get_the_fd (SL_TICKET ticket);
|
---|
| 109 |
|
---|
| 110 | static 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 | */
|
---|
| 150 | int 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 | {
|
---|
[132] | 181 | char dir[SH_PATHBUF];
|
---|
| 182 |
|
---|
[1] | 183 | sh_ext_tas_init(&task);
|
---|
[132] | 184 | p = sh_unix_getUIDdir (SH_ERR_ERR, task.run_user_uid, dir, sizeof(dir));
|
---|
[1] | 185 | if (p)
|
---|
| 186 | {
|
---|
| 187 | (void) sh_ext_tas_add_envv (&task, _("HOME"), p);
|
---|
| 188 | }
|
---|
| 189 | (void) sh_ext_tas_add_envv (&task, _("SHELL"),
|
---|
| 190 | _("/bin/sh"));
|
---|
| 191 | (void) sh_ext_tas_add_envv (&task, _("PATH"),
|
---|
| 192 | _("/sbin:/usr/sbin:/bin:/usr/bin"));
|
---|
| 193 | if (sh.timezone != NULL)
|
---|
| 194 | {
|
---|
| 195 | (void) sh_ext_tas_add_envv(&task, "TZ", sh.timezone);
|
---|
| 196 | }
|
---|
| 197 | if (prelink_path == NULL)
|
---|
| 198 | {
|
---|
| 199 | sh_ext_tas_command(&task, _("/usr/sbin/prelink"));
|
---|
| 200 | (void) sh_ext_tas_add_argv(&task, _("/usr/sbin/prelink"));
|
---|
| 201 | }
|
---|
| 202 | else
|
---|
| 203 | {
|
---|
| 204 | sh_ext_tas_command(&task, prelink_path);
|
---|
| 205 | (void) sh_ext_tas_add_argv(&task, prelink_path);
|
---|
| 206 | }
|
---|
| 207 | args_filled = sh_ext_tas_add_argv(&task, _("--verify"));
|
---|
| 208 |
|
---|
| 209 | if (prelink_hash != NULL)
|
---|
| 210 | {
|
---|
| 211 | (void) sl_strlcpy(task.checksum, prelink_hash, KEY_LEN+1);
|
---|
| 212 | }
|
---|
| 213 | task.rw = 'r';
|
---|
| 214 | task.fork_twice = S_FALSE;
|
---|
| 215 |
|
---|
| 216 | sh_prelink_fd(&task);
|
---|
| 217 |
|
---|
| 218 | init = S_TRUE;
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /* rm filename arg if set; fill in filename
|
---|
| 222 | */
|
---|
| 223 | if (args_filled == 3)
|
---|
| 224 | args_filled = sh_ext_tas_rm_argv(&task);
|
---|
| 225 | if (args_filled == 2)
|
---|
| 226 | args_filled = sh_ext_tas_add_argv(&task, path);
|
---|
| 227 | else
|
---|
| 228 | {
|
---|
| 229 | sh_error_handle(SH_ERR_ERR, FIL__, __LINE__, args_filled, MSG_E_SUBGEN,
|
---|
| 230 | _("Bad argument count"), _("sh_prelink_run"));
|
---|
| 231 | SL_RETURN ((-1), _("sh_prelink_run"));
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* open pipe
|
---|
| 235 | */
|
---|
| 236 | status = sh_ext_popen(&task);
|
---|
| 237 | if (status != 0)
|
---|
| 238 | {
|
---|
| 239 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, status, MSG_E_SUBGEN,
|
---|
| 240 | _("Could not open pipe"), _("sh_prelink_run"));
|
---|
| 241 | SL_RETURN ((-1), _("sh_prelink_run"));
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | if (SL_ISERROR(task.pipeTI))
|
---|
| 245 | {
|
---|
| 246 | sh_error_handle(SH_ERR_ALL, FIL__, __LINE__, task.pipeTI, MSG_E_SUBGEN,
|
---|
| 247 | _("No valid ticket"), _("sh_prelink_run"));
|
---|
| 248 | SL_RETURN ((-1), _("sh_prelink_run"));
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /* ignore SIGPIPE (instead get EPIPE if connection is closed)
|
---|
| 252 | */
|
---|
| 253 | new_act.sa_handler = SIG_IGN;
|
---|
| 254 | (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &new_act, &old_act);
|
---|
| 255 |
|
---|
| 256 | /* read from pipe
|
---|
| 257 | */
|
---|
| 258 | tiger_fd = task.pipeTI;
|
---|
[8] | 259 |
|
---|
| 260 | sl_read_timeout_prep (task.pipeTI);
|
---|
| 261 |
|
---|
[22] | 262 | sl_strlcpy(file_hash,
|
---|
[107] | 263 | sh_tiger_generic_hash (path, TIGER_FD, TIGER_NOLIM, alert_timeout),
|
---|
[22] | 264 | KEY_LEN+1);
|
---|
[1] | 265 |
|
---|
| 266 | /* restore old signal handler
|
---|
| 267 | */
|
---|
| 268 | (void) retry_sigaction (FIL__, __LINE__, SIGPIPE, &old_act, NULL);
|
---|
| 269 |
|
---|
| 270 | /* close pipe and return exit status
|
---|
| 271 | */
|
---|
| 272 | status = sh_ext_pclose(&task);
|
---|
| 273 | SL_RETURN ((status), _("sh_prelink_run"));
|
---|
| 274 | }
|
---|
| 275 | /* defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
|
---|
| 276 | */
|
---|
| 277 | #endif
|
---|