forked from acassen/keepalived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.c
1221 lines (1011 loc) · 30.8 KB
/
notify.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Soft: Keepalived is a failover program for the LVS project
* <www.linuxvirtualserver.org>. It monitor & manipulate
* a loadbalanced server pool using multi-layer checks.
*
* Part: Forked system call to launch an extra script.
*
* Author: Alexandre Cassen, <[email protected]>
*
* 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.
*
* 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.
*
* Copyright (C) 2001-2017 Alexandre Cassen, <[email protected]>
*/
#include "config.h"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <grp.h>
#include <string.h>
#include <sys/stat.h>
#include <pwd.h>
#include <sys/resource.h>
#include <limits.h>
#include <sys/prctl.h>
#include "notify.h"
#include "signals.h"
#include "logger.h"
#include "utils.h"
#include "process.h"
#include "parser.h"
#include "keepalived_magic.h"
#include "scheduler.h"
/* Save our uid/gid */
uid_t our_uid;
gid_t our_gid;
/* Default user/group for script execution */
static uid_t default_script_uid;
static gid_t default_script_gid;
/* Have we got a default user OK? */
static bool default_script_uid_set = false;
static bool default_user_fail = false;
/* Script security enabled */
bool script_security = false;
/* Buffer length needed for getpwnam_r/getgrname_r */
static size_t getpwnam_buf_len;
static char *path;
static bool path_is_malloced;
static bool use_symlinks;
/* Buffer for expanding notify script commands */
static char cmd_str_buf[MAXBUF];
void
set_symlinks(bool state)
{
use_symlinks = state;
}
static bool
set_script_env(uid_t uid, gid_t gid)
{
if (gid != our_gid) {
if (setgid(gid) < 0) {
log_message(LOG_ALERT, "Couldn't setgid: %u (%m)", gid);
return true;
}
}
/* Clear any extra supplementary groups */
if (setgroups(1, &gid) < 0) {
log_message(LOG_ALERT, "Couldn't setgroups: %u (%m)", gid);
return true;
}
if (uid != our_uid) {
if (setuid(uid) < 0) {
log_message(LOG_ALERT, "Couldn't setuid: %u (%m)", uid);
return true;
}
}
/* Prepare for invoking process/script */
signal_handler_script();
set_std_fd(false);
return false;
}
const char *
cmd_str_r(const notify_script_t *script, char *buf, size_t len)
{
char *str_p;
int i;
size_t str_len;
str_p = buf;
for (i = 0; i < script->num_args; i++) {
/* Check there is enough room for the next word */
str_len = strlen(script->args[i]);
if (str_p + str_len + 2 + (i ? 1 : 0) >= buf + len)
return NULL;
if (i)
*str_p++ = ' ';
/* Allow special case of bash script which is redirection only to
* test for file existence. */
if (i || (script->args[i][0] != '<' && script->args[i][0] != '>'))
*str_p++ = '\'';
strcpy(str_p, script->args[i]);
str_p += str_len;
/* Close opening ' if we added one */
if (i || (script->args[i][0] != '<' && script->args[i][0] != '>'))
*str_p++ = '\'';
}
*str_p = '\0';
return buf;
}
const char *
cmd_str(const notify_script_t *script)
{
size_t len;
int i;
for (i = 0, len = 0; i < script->num_args; i++)
len += strlen(script->args[i]) + 3; /* Add two ', and trailing space (or null for last arg) */
if (len > sizeof cmd_str_buf)
return NULL;
return cmd_str_r(script, cmd_str_buf, sizeof cmd_str_buf);
}
int
system_call_script(thread_master_t *m, thread_func_t func, void * arg, unsigned long timer, const notify_script_t* script)
{
pid_t pid;
const char *str;
int retval;
union non_const_args args;
/* Daemonization to not degrade our scheduling timer */
#ifdef ENABLE_LOG_TO_FILE
if (log_file_name)
flush_log_file();
#endif
pid = fork();
if (pid < 0) {
/* fork error */
log_message(LOG_INFO, "Failed fork process");
return -1;
}
if (pid) {
/* parent process */
if (func) {
thread_add_child(m, func, arg, pid, timer);
#ifdef _SCRIPT_DEBUG_
if (do_script_debug)
log_message(LOG_INFO, "Running script %s with pid %d, timer %lu.%6.6lu", script->args[0], pid, timer / TIMER_HZ, timer % TIMER_HZ);
#endif
}
return 0;
}
/* Child process */
reset_process_priorities();
#ifdef _MEM_CHECK_
skip_mem_dump();
#endif
if (set_script_env(script->uid, script->gid))
exit(0);
/* Move us into our own process group, so if the script needs to be killed
* all its child processes will also be killed. */
setpgid(0, 0);
if (script->flags & SC_EXECABLE) {
/* If keepalived dies, we want the script to die */
prctl(PR_SET_PDEATHSIG, SIGTERM);
args.args = script->args; /* Note: we are casting away constness, since execve parameter type is wrong */
execve(script->path ? script->path : script->args[0], args.execve_args, environ);
/* error */
log_message(LOG_ALERT, "Error exec-ing command '%s', error %d: %m", script->path ? script->path : script->args[0], errno);
} else {
retval = system(str = cmd_str(script));
if (retval == -1) {
log_message(LOG_ALERT, "Error exec-ing command: %s", str);
exit(0);
}
if (WIFEXITED(retval)) {
if (WEXITSTATUS(retval) == 127) {
/* couldn't find command */
log_message(LOG_ALERT, "Couldn't find command: %s", str);
}
else if (WEXITSTATUS(retval) == 126) {
/* couldn't find command */
log_message(LOG_ALERT, "Couldn't execute command: %s", str);
}
else
exit(WEXITSTATUS(retval));
exit(0);
}
if (WIFSIGNALED(retval))
kill(getpid(), WTERMSIG(retval));
}
exit(0); /* Script errors aren't server errors */
}
/* Execute external script/program */
int
notify_exec(const notify_script_t *script)
{
return system_call_script(NULL, NULL, NULL, 0, script);
}
static void
fifo_open(notify_fifo_t* fifo, thread_func_t script_exit, const char *type)
{
int ret;
int sav_errno;
if (fifo->name) {
sav_errno = 0;
if (!(ret = mkfifo(fifo->name, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) {
fifo->created_fifo = true;
if ((fifo->uid != our_uid || fifo->gid != our_gid) &&
chown(fifo->name, our_uid != fifo->uid ? fifo->uid : (uid_t)-1, our_gid != fifo->gid ? fifo->gid : (gid_t)-1))
log_message(LOG_INFO, "Failed to set uid:gid for fifo %s", fifo->name);
} else {
sav_errno = errno;
if (sav_errno != EEXIST)
log_message(LOG_INFO, "Unable to create %snotify fifo %s", type, fifo->name);
}
if (!sav_errno || sav_errno == EEXIST) {
/* Run the notify script if there is one */
if (fifo->script)
system_call_script(master, script_exit, fifo, TIMER_NEVER, fifo->script);
/* Now open the fifo */
if ((fifo->fd = open(fifo->name, O_RDWR | O_CLOEXEC | O_NONBLOCK | O_NOFOLLOW)) == -1) {
log_message(LOG_INFO, "Unable to open %snotify fifo %s - errno %d", type, fifo->name, errno);
if (fifo->created_fifo) {
unlink(fifo->name);
fifo->created_fifo = false;
}
}
}
if (fifo->fd == -1) {
FREE_CONST(fifo->name);
fifo->name = NULL;
}
}
}
void
notify_fifo_open(notify_fifo_t* global_fifo, notify_fifo_t* fifo, thread_func_t script_exit, const char *type)
{
/* Open the global FIFO if specified */
if (global_fifo->name)
fifo_open(global_fifo, script_exit, "");
/* Now the specific FIFO */
if (fifo->name)
fifo_open(fifo, script_exit, type);
}
static void
fifo_close(notify_fifo_t* fifo)
{
/* We unlink the fifo first, so that a script that
* loops reopening the fifo if it is closed cannot
* reopen this fifo. */
if (fifo->created_fifo)
unlink(fifo->name);
if (fifo->fd != -1) {
close(fifo->fd);
fifo->fd = -1;
}
}
void
notify_fifo_close(notify_fifo_t* global_fifo, notify_fifo_t* fifo)
{
if (global_fifo->fd != -1)
fifo_close(global_fifo);
fifo_close(fifo);
}
static void
child_killed_reload(thread_ref_t thread)
{
/* If the child didn't die, then force it */
if (thread->type == THREAD_CHILD_TIMEOUT)
kill(-getpgid(thread->u.c.pid), SIGKILL);
}
void
child_killed_thread(thread_ref_t thread)
{
thread_master_t *m = thread->master;
/* If the child didn't die, then force it */
if (thread->type == THREAD_CHILD_TIMEOUT)
kill(-getpgid(thread->u.c.pid), SIGKILL);
/* If all children have died, we can now complete the
* termination process */
if (!m->child.rb_root.rb_node && !m->shutdown_timer_running)
thread_add_terminate_event(m);
}
void
script_killall(thread_master_t *m, int signo, bool shutting_down)
{
thread_t *thread;
pid_t p_pgid, c_pgid;
p_pgid = getpgid(0);
rb_for_each_entry_cached(thread, &m->child, n) {
c_pgid = getpgid(thread->u.c.pid);
if (c_pgid != p_pgid)
kill(-c_pgid, signo);
else {
log_message(LOG_INFO, "Child process %d in our process group %d", c_pgid, p_pgid);
kill(thread->u.c.pid, signo);
}
}
/* We want to timeout the killed children in 1 second */
if (signo != SIGKILL)
thread_children_reschedule(m, shutting_down ? child_killed_thread : child_killed_reload, TIMER_HZ);
}
static bool
is_executable(const struct stat *buf, uid_t uid, gid_t gid)
{
return (uid == 0 && buf->st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) ||
(uid == buf->st_uid && buf->st_mode & S_IXUSR) ||
(uid != buf->st_uid &&
((gid == buf->st_gid && buf->st_mode & S_IXGRP) ||
(gid != buf->st_gid && buf->st_mode & S_IXOTH)));
}
static void
replace_cmd_name(notify_script_t *script, const char *new_path)
{
size_t len;
const char * const *wp = &script->args[1];
size_t num_words = 1;
char **params;
char **word_ptrs;
char *words;
/* Avoid gcc warning: to be safe all intermediate pointers in cast from ‘char **’
* to ‘const char **’ must be ‘const’ qualified
* What we want to assign to is a pointer (therefore non const) to an array of pointers
* which can be modified, therefore non-const, to const strings, i.e. const char **;
* but if assign a char ** to a const char ** we get the above warning
*/
union {
char **params;
const char **cparams;
} args;
len = strlen(new_path) + 1;
while (*wp)
len += strlen(*wp++) + 1;
num_words = (script->args[0] - PTR_CAST_CONST(char, &script->args[0])) - 1;
params = word_ptrs = MALLOC((num_words + 1) * sizeof(char *) + len);
words = PTR_CAST(char, ¶ms[num_words + 1]);
strcpy(words, new_path);
*(word_ptrs++) = words;
words += strlen(words) + 1;
wp = &script->args[1];
while (*wp) {
strcpy(words, *wp);
*(word_ptrs++) = words;
words += strlen(*wp) + 1;
wp++;
}
*word_ptrs = NULL;
FREE_CONST(script->args);
args.params = params;
script->args = args.cparams;
}
/* The following function is essentially __execve_common() from glibc posix/execvpe.c */
static int
find_path(notify_script_t *script)
{
size_t filename_len;
size_t file_len;
size_t path_len;
const char *file = script->args[0];
struct stat buf;
int ret;
int ret_val = ENOENT;
int sgid_num;
gid_t *sgid_list = NULL;
const char *subp;
bool got_eacces = false;
const char *p;
/* We check the simple case first. */
if (*file == '\0')
return ENOENT;
filename_len = strlen(file);
if (filename_len >= PATH_MAX)
return ENAMETOOLONG;
/* Don't search when it contains a slash. */
if (strchr (file, '/') != NULL)
return 0;
/* Get the path if we haven't already done so, and if that doesn't
* exist, use CS_PATH */
if (!path) {
path = getenv ("PATH");
if (!path) {
size_t cs_path_len;
path = MALLOC(cs_path_len = confstr(_CS_PATH, NULL, 0));
confstr(_CS_PATH, path, cs_path_len);
path_is_malloced = true;
}
}
/* Although GLIBC does not enforce NAME_MAX, we set it as the maximum
size to avoid unbounded stack allocation. Same applies for
PATH_MAX. */
file_len = strnlen (file, NAME_MAX + 1);
path_len = strnlen (path, PATH_MAX - 1) + 1;
if (file_len > NAME_MAX)
return ENAMETOOLONG;
if (script->uid != our_uid || script->gid != our_gid) {
/* Set file access to the relevant uid/gid */
if (script->gid != our_gid) {
if (setegid(script->gid)) {
log_message(LOG_INFO, "Unable to set egid to %u (%m)", script->gid);
return EACCES;
}
}
if (script->uid != our_uid) {
if (seteuid(script->uid)) {
log_message(LOG_INFO, "Unable to set euid to %u (%m)", script->uid);
ret_val = EACCES;
goto exit;
}
}
/* Get our supplementary groups */
sgid_num = getgroups(0, NULL);
if (sgid_num == -1) {
log_message(LOG_INFO, "Unable to get number of supplementary gids (%m)");
ret_val = EACCES;
goto exit;
}
sgid_list = MALLOC(((size_t)sgid_num + 1) * sizeof(gid_t));
sgid_num = getgroups(sgid_num, sgid_list);
sgid_list[sgid_num++] = our_gid;
/* Clear the supplementary group list */
if (setgroups(1, &script->gid)) {
log_message(LOG_INFO, "Unable to set supplementary gids (%m)");
ret_val = EACCES;
goto exit;
}
}
for (p = path; ; p = subp)
{
subp = strchrnul (p, ':');
/* PATH is larger than PATH_MAX and thus potentially larger than
the stack allocation. */
if (subp >= p + path_len) {
/* There are no more paths, bail out. */
if (*subp == '\0') {
ret_val = ENOENT;
goto exit;
}
/* Otherwise skip to next one. */
continue;
}
/* Use the current path entry, plus a '/' if nonempty, plus the file to execute. */
char *buffer = MALLOC(path_len + file_len + 1);
char *pend = mempcpy (buffer, p, (size_t)(subp - p));
*pend = '/';
memcpy (pend + (p < subp), file, file_len + 1);
ret = stat (buffer, &buf);
if (!ret) {
if (!S_ISREG(buf.st_mode))
errno = EACCES;
else if (!is_executable(&buf, script->uid, script->gid)) {
errno = EACCES;
} else {
/* Success */
log_message(LOG_INFO, "WARNING - script `%s` resolved by path search to `%s`. Please specify full path.", script->args[0], buffer);
/* Copy the found file name, and any parameters */
replace_cmd_name(script, buffer);
ret_val = 0;
got_eacces = false;
FREE(buffer);
goto exit;
}
}
FREE(buffer);
switch (errno)
{
case ENOEXEC:
case EACCES:
/* Record that we got a 'Permission denied' error. If we end
up finding no executable we can use, we want to diagnose
that we did find one but were denied access. */
if (!ret)
got_eacces = true;
case ENOENT:
case ESTALE:
case ENOTDIR:
/* Those errors indicate the file is missing or not executable
by us, in which case we want to just try the next path
directory. */
case ENODEV:
case ETIMEDOUT:
/* Some strange filesystems like AFS return even
stranger error numbers. They cannot reasonably mean
anything else so ignore those, too. */
break;
default:
/* Some other error means we found an executable file, but
something went wrong accessing it; return the error to our
caller. */
ret_val = -1;
goto exit;
}
if (*subp++ == '\0')
break;
}
exit:
/* Restore root euid/egid */
if (script->gid != our_gid && setegid(our_gid))
log_message(LOG_INFO, "Unable to restore egid after script search (%m)");
if (script->uid != our_uid && seteuid(our_uid))
log_message(LOG_INFO, "Unable to restore euid after script search (%m)");
/* restore supplementary groups */
if (sgid_list) {
if (setgroups((size_t)sgid_num, sgid_list))
log_message(LOG_INFO, "Unable to restore supplementary groups after script search (%m)");
FREE(sgid_list);
}
/* We tried every element and none of them worked. */
if (got_eacces) {
/* At least one failure was due to permissions, so report that error. */
return EACCES;
}
return ret_val;
}
static int
check_security(const char *filename, bool using_script_security)
{
const char *next;
char *slash;
int ret;
struct stat buf;
int flags = 0;
char *filename_copy;
next = filename;
while (next) {
slash = strchrnul(next, '/');
if (*slash)
next = slash + 1;
else {
slash = NULL;
next = NULL;
}
if (slash) {
/* We want to check '/' for first time around */
if (slash == filename)
slash++;
filename_copy = STRNDUP(filename, slash - filename);
}
ret = fstatat(0, slash ? filename_copy : filename, &buf, AT_SYMLINK_NOFOLLOW);
/* Restore the full path name */
if (slash)
FREE(filename_copy);
if (ret) {
if (errno == EACCES || errno == ELOOP || errno == ENOENT || errno == ENOTDIR)
log_message(LOG_INFO, "check_script_secure could not find script '%s' - disabling", filename);
else
log_message(LOG_INFO, "check_script_secure('%s') returned errno %d - %s - disabling", filename, errno, strerror(errno));
return flags | SC_NOTFOUND;
}
/* If it is not the last item, it must be a directory. If it is the last item
* it must be a file or a symbolic link. */
if ((slash && !S_ISDIR(buf.st_mode)) ||
(!slash &&
!S_ISREG(buf.st_mode) &&
!S_ISLNK(buf.st_mode))) {
log_message(LOG_INFO, "Wrong file type found in script path '%s'.", filename);
return flags | SC_INHIBIT;
}
if (buf.st_uid || /* Owner is not root */
(((S_ISDIR(buf.st_mode) && /* A directory without the sticky bit set */
!(buf.st_mode & S_ISVTX)) ||
S_ISREG(buf.st_mode)) && /* This is a file */
((buf.st_gid && buf.st_mode & S_IWGRP) || /* Group is not root and group write permission */
buf.st_mode & S_IWOTH))) { /* World has write permission */
log_message(LOG_INFO, "Unsafe permissions found for script '%s'%s.", filename, using_script_security ? " - disabling" : "");
flags |= SC_INSECURE;
return flags | (using_script_security ? SC_INHIBIT : 0);
}
}
return flags;
}
static char *
clean_path(const char *old_argv)
{
char *new_argv = MALLOC(strlen(old_argv) + 1 + 1); // We can add an initial '/'
const char *slash, *next_slash;
char *previous_slash;
const char *ip = old_argv;
char *op = new_argv;
/* Remove leading ./ and ../ */
if (!strncmp(ip, "./", 2))
ip += 1;
else if (!strncmp(ip, "../", 3))
ip += 2;
/* Remove any leading /../ and /./ recursively */
while (!strncmp(ip, "/../", 4) || !strncmp(ip, "/./", 3) || !strncmp(ip, "//", 2))
ip += ip[1] == '/' ? 1 : ip[2] == '/' ? 2 : 3;
if (*ip != '/')
*op++ = '/';
slash = strchr(ip, '/');
if (!slash)
strcpy(op, ip);
else if (slash > ip) {
strncpy(op, ip, slash - ip);
op += slash - ip;
}
for (; slash; slash = next_slash) {
next_slash = strchr(slash + 1, '/');
if (!next_slash) {
strcpy(op, slash);
break;
}
/* We are looking for '//', '/./' or '/../' */
if (next_slash > slash + 3 ||
(slash[1] != '.' && slash[1] != '/') ||
(slash[1] == '.' &&
(slash[2] != '.' && slash[2] != '/'))) {
strncpy(op, slash, next_slash - slash);
op += next_slash - slash;
continue;
}
/* We have found one of them */
if (next_slash == slash + 1) {
/* // */
ip++;
continue;
}
if (next_slash == slash + 2) {
/* /./ */
ip += 2;
continue;
}
/* /../ - we must remove previous element */
previous_slash = strrchr(new_argv, '/');
if (!previous_slash)
op = new_argv;
else
op = previous_slash;
*op = '\0';
}
if (!strcmp(old_argv, new_argv)) {
FREE(new_argv);
return NULL;
}
return new_argv;
}
unsigned
check_script_secure(notify_script_t *script,
#ifndef _HAVE_LIBMAGIC_
__attribute__((unused))
#endif
magic_t magic)
{
unsigned flags;
int ret;
struct stat file_buf;
bool need_script_protection = false;
const char *real_path;
const char *new_argv_0;
union {
char *nc; // needed for free()
const char *c;
} sav_path;
int sav_errno;
int sav_death_sig;
if (!script)
return 0;
/* If the script starts "</" (possibly with white space between
* the '<' and '/'), it is checking for a file being openable,
* so it won't be executed */
if (script->args[0][0] == '<' &&
script->args[0][strspn(script->args[0] + 1, " \t") + 1] == '/')
return SC_SYSTEM;
if (!strchr(script->args[0], '/')) {
/* It is a bare file name, so do a path search */
if ((ret = find_path(script))) {
if (ret == EACCES)
log_message(LOG_INFO, "Permissions failure for script %s in path - disabling", script->args[0]);
else
log_message(LOG_INFO, "Cannot find script %s in path - disabling", script->args[0]);
return SC_NOTFOUND;
}
}
/* Check script accessible by the user running it */
if (script->gid != our_gid || script->uid != our_uid) {
/* Save parent death signal */
prctl(PR_GET_PDEATHSIG, &sav_death_sig);
if ((script->gid != our_gid && setegid(script->gid)) ||
(script->uid != our_uid && seteuid(script->uid))) {
log_message(LOG_INFO, "Unable to set uid:gid %u:%u for script %s - disabling", script->uid, script->gid, script->args[0]);
if ((script->uid != our_uid && seteuid(our_uid)) ||
(script->gid != our_gid && setegid(our_gid)))
log_message(LOG_INFO, "Unable to restore uid:gid from %u:%u %u:%u after script %s", our_uid, our_gid, script->uid, script->gid, script->args[0]);
return SC_INHIBIT;
}
}
real_path = realpath(script->args[0], NULL);
sav_errno = errno;
if (script->gid != our_gid || script->uid != our_uid) {
if ((script->gid != our_gid && setegid(our_gid)) ||
(script->uid != our_uid && seteuid(our_uid)))
log_message(LOG_INFO, "Unable to restore uid:gid %u:%u from %u:%u after checking script %s", our_uid, our_gid, script->uid, script->gid, script->args[0]);
/* Restore parent death signal */
prctl(PR_SET_PDEATHSIG, sav_death_sig);
/* Check the parent didn't die in the window when PDEATHSIG was not set */
if (!__test_bit(CONFIG_TEST_BIT, &debug) && main_pid != getppid())
kill(getpid(), SIGTERM);
}
if (!real_path) {
log_message(LOG_INFO, "Script %s cannot be accessed - %s", script->args[0], strerror(sav_errno));
return SC_NOTFOUND;
}
/* It is much easier to ensure that real_path is part of
* keepalived's malloc handling. */
sav_path.c = real_path;
real_path = STRDUP(real_path);
free(sav_path.nc); /* malloc'd returned by realpath() */
/* Get the permissions for the file itself */
if (stat(real_path, &file_buf)) {
log_message(LOG_INFO, "Unable to access script `%s` - disabling", script->args[0]);
FREE_CONST(real_path);
return SC_NOTFOUND;
}
if (use_symlinks || strcmp(real_path, script->args[0])) {
/* Remove /./, /../, repeated /'s from argv[0] */
new_argv_0 = clean_path(script->args[0]);
if (new_argv_0) {
/* The path name is different */
replace_cmd_name(script, new_argv_0);
FREE_CONST_ONLY(new_argv_0);
}
}
if (strcmp(real_path, script->args[0]) && !use_symlinks) {
script->path = real_path;
real_path = NULL;
}
flags = SC_ISSCRIPT;
/* We have the final file. Check if root is executing it, or it is set uid/gid root. */
if (is_executable(&file_buf, script->uid, script->gid)) {
flags |= SC_EXECUTABLE;
if (script->uid == 0 || script->gid == 0 ||
(file_buf.st_uid == 0 && (file_buf.st_mode & S_IXUSR) && (file_buf.st_mode & S_ISUID)) ||
(file_buf.st_gid == 0 && (file_buf.st_mode & S_IXGRP) && (file_buf.st_mode & S_ISGID)))
need_script_protection = true;
} else
log_message(LOG_INFO, "WARNING - script '%s' is not executable for uid:gid %u:%u - disabling.", script->args[0], script->uid, script->gid);
/* Default to execable */
script->flags |= SC_EXECABLE;
#ifdef _HAVE_LIBMAGIC_
if (magic && flags & SC_EXECUTABLE) {
const char *magic_desc = magic_file(magic, real_path ? real_path : script->path ? script->path : script->args[0]);
if (!strstr(magic_desc, " executable") &&
!strstr(magic_desc, " shared object")) {
log_message(LOG_INFO, "Please add a #! shebang to script %s", script->args[0]);
script->flags &= ~SC_EXECABLE;
}
}
#endif
if (real_path)
FREE_CONST(real_path);
if (need_script_protection) {
/* Make sure that all parts of the path(s) are not non-root writable */
flags |= check_security(script->args[0], script_security);
if (script->path)
flags |= check_security(script->path, script_security);
}
return flags;
}
unsigned
check_notify_script_secure(notify_script_t **script_p, magic_t magic)
{
unsigned flags;
notify_script_t *script = *script_p;
if (!script)
return 0;
flags = check_script_secure(script, magic);
/* Mark not to run if needs inhibiting */
if ((flags & (SC_INHIBIT | SC_NOTFOUND)) ||
!(flags & SC_EXECUTABLE))
free_notify_script(script_p);
return flags;
}
static void
set_pwnam_buf_len(void)
{
long buf_len;
/* Get buffer length needed for getpwnam_r/getgrnam_r */
if ((buf_len = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1)
getpwnam_buf_len = 1024; /* A safe default if no value is returned */
else
getpwnam_buf_len = (size_t)buf_len;
if ((buf_len = sysconf(_SC_GETGR_R_SIZE_MAX)) != -1 &&
(size_t)buf_len > getpwnam_buf_len)
getpwnam_buf_len = (size_t)buf_len;
}
static bool
set_uid_gid(const char *username, const char *groupname, uid_t *uid_p, gid_t *gid_p)
{
uid_t uid;
gid_t gid;
struct passwd pwd;
struct passwd *pwd_p;
struct group grp;
struct group *grp_p;
int ret;
char *buf;
if (!getpwnam_buf_len)
set_pwnam_buf_len();
buf = MALLOC(getpwnam_buf_len);
if ((ret = getpwnam_r(username, &pwd, buf, getpwnam_buf_len, &pwd_p))) {
log_message(LOG_INFO, "Unable to resolve script username '%s' - ignoring", username);
FREE(buf);
return true;
}
if (!pwd_p) {
log_message(LOG_INFO, "Script user '%s' does not exist", username);
FREE(buf);
return true;
}
uid = pwd.pw_uid;
gid = pwd.pw_gid;
if (groupname) {
if ((ret = getgrnam_r(groupname, &grp, buf, getpwnam_buf_len, &grp_p))) {
log_message(LOG_INFO, "Unable to resolve script group name '%s' - ignoring", groupname);
FREE(buf);
return true;
}
if (!grp_p) {
log_message(LOG_INFO, "Script group '%s' does not exist", groupname);
FREE(buf);
return true;
}
gid = grp.gr_gid;
}
*uid_p = uid;
*gid_p = gid;