forked from scanmem/scanmem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.c
1782 lines (1530 loc) · 52.9 KB
/
handlers.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
/*
Specific command handling.
Copyright (C) 2006,2007,2009 Tavis Ormandy <[email protected]>
Copyright (C) 2009 Eli Dupree <[email protected]>
Copyright (C) 2009,2010 WANG Lu <[email protected]>
Copyright (C) 2014-2016 Sebastian Parschauer <[email protected]>
This file is part of libscanmem.
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "config.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <signal.h>
#include <assert.h>
#include <setjmp.h>
#include <alloca.h>
#include <strings.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <sys/time.h>
#include <limits.h> /* to determine the word width */
#include <errno.h>
#include <inttypes.h>
#include <ctype.h>
#include "common.h"
#include "commands.h"
#include "endianness.h"
#include "handlers.h"
#include "interrupt.h"
#include "scanmem.h"
#include "scanroutines.h"
#include "sets.h"
#include "show_message.h"
#define USEPARAMS() ((void) vars, (void) argv, (void) argc) /* macro to hide gcc unused warnings */
/*
* This file defines all the command handlers used, each one is registered using
* registercommand(). When a matching command is entered, the commandline is
* tokenized and parsed into an argv/argc.
*
* argv[0] will contain the command entered, so one handler can handle multiple
* commands by checking what's in there. You still need seperate documentation
* for each command when you register it.
*
* Most commands will also need some documentation, see handlers.h for the format.
*
* Commands are allowed to read and modify settings in the vars structure.
*
*/
#define calloca(x,y) (memset(alloca((x) * (y)), 0x00, (x) * (y)))
/* try to determine the size of a pointer */
#ifndef ULONG_MAX
#warning ULONG_MAX is not defined!
#endif
#if ULONG_MAX == 4294967295UL
#define POINTER_FMT "%8lx"
#elif ULONG_MAX == 18446744073709551615UL
#define POINTER_FMT "%12lx"
#else
#define POINTER_FMT "%12lx"
#endif
/* pager support */
static FILE *get_pager(FILE *fallback_output)
{
const char *pager;
pid_t pgpid;
int pgret;
int pgpipe[2];
bool pgcmdfail = false;
FILE *retfd = NULL;
char *const emptyvec[1] = { NULL };
assert(fallback_output != NULL);
if (sm_globals.options.backend)
return fallback_output;
if ((pager = getenv("PAGER")) == NULL || *pager == '\0') {
show_warn("get_pager(): couldn't get $PAGER, falling back to `more`\n");
pager = "more";
}
if (pipe2(pgpipe, O_NONBLOCK) == -1) {
show_error("get_pager(): pipe2() error `%s`. falling back to normal output\n", strerror(errno));
return fallback_output;
}
/*
* we write here to ensure we will always
* have something to read() into pgcmdfail.
*/
write(pgpipe[1], "", 1);
/* XXX: is $PATH modified prior? */
retry:
switch ((pgpid = fork())) {
case -1:
show_warn("get_pager(): fork() failed. falling back to normal output\n");
return fallback_output;
case 0:
execvp(pager, emptyvec);
/*
* if we got here, it means execvp() failed.
* errno contains the error, so pass it back
* up to parent. we use a pipe here to let
* the parent know that we are indeed returning
* the return value of the failed execvp().
*/
char nullbuf;
/* read() to empty pipe */
read(pgpipe[0], &nullbuf, 1);
write(pgpipe[1], "1", 2);
exit(errno);
/* NOTREACHED */
default:
if (waitpid(pgpid, &pgret, 0) == -1) {
show_debug("pager: waitpid() error `%s`\n", strerror(errno));
show_warn("pager: waitpid() error. falling back to normal output\n");
return fallback_output;
} else {
pgret = WEXITSTATUS(pgret);
if (read(pgpipe[0], &pgcmdfail, 1) == -1) {
show_error("pager: pipe read() error `%s`. falling back to normal output\n", strerror(errno));
return fallback_output;
}
if (pgcmdfail) {
show_debug("pager: execvp(pg=%s) ret -> %d (%s)\n", pager, pgret, strerror(pgret));
if (!strcmp(pager, "more")) {
show_warn("pager: sh failed to execute more. falling back to normal output\n");
return fallback_output;
} else {
show_warn("pager: sh failed to execute `%s`. trying `more`...\n", pager);
pager = "more";
goto retry;
}
}
}
}
if ((retfd = popen(pager, "w")) == NULL) {
show_warn("pager: couldn't popen() pager, falling back to normal output\n");
return fallback_output;
}
/*
* we need to ignore SIGPIPE in case the pager exits wihout draining the
* pipe (less seems to do this if you exit a large listing without
* scrolling down)
*/
signal(SIGPIPE, SIG_IGN);
assert(retfd != NULL);
return retfd;
}
static inline void close_pager(FILE *pager)
{
if (pager != stdout && pager != stderr) {
if (pclose(pager) == -1 && errno != EPIPE)
show_warn("pclose() error: %s\n", strerror(errno));
signal(SIGPIPE, SIG_DFL);
}
}
bool handler__set(globals_t * vars, char **argv, unsigned argc)
{
unsigned block, seconds = 1;
char *delay = NULL;
bool cont = false;
struct setting {
char *matchids;
char *value;
unsigned seconds;
} *settings = NULL;
assert(argc != 0);
assert(argv != NULL);
assert(vars != NULL);
if (argc < 2) {
show_error("expected an argument, type `help set` for details.\n");
return false;
}
/* supporting `set` for bytearray will cause annoying syntax problems... */
if ((vars->options.scan_data_type == BYTEARRAY)
||(vars->options.scan_data_type == STRING))
{
show_error("`set` is not supported for bytearray or string, use `write` instead.\n");
return false;
}
/* check if there are any matches */
if (vars->num_matches == 0) {
show_error("no matches are known.\n");
return false;
}
/* --- parse arguments into settings structs --- */
settings = calloca(argc - 1, sizeof(struct setting));
/* parse every block into a settings struct */
for (block = 0; block < argc - 1; block++) {
/* first separate the block into matches and value, which are separated by '=' */
if ((settings[block].value = strchr(argv[block + 1], '=')) == NULL) {
/* no '=' found, whole string must be the value */
settings[block].value = argv[block + 1];
} else {
/* there is a '=', value+1 points to value string. */
/* use strndupa() to copy the matchids into a new buffer */
settings[block].matchids =
strndupa(argv[block + 1],
(size_t) (settings[block].value++ - argv[block + 1]));
}
/* value points to the value string, possibly with a delay suffix */
/* matchids points to the match-ids (possibly multiple) or NULL */
/* now check for a delay suffix (meaning continuous mode), eg 0xff/10 */
if ((delay = strchr(settings[block].value, '/')) != NULL) {
char *end = NULL;
/* parse delay count */
settings[block].seconds = strtoul(delay + 1, &end, 10);
if (*(delay + 1) == '\0') {
/* empty delay count, eg: 12=32/ */
show_error("you specified an empty delay count, `%s`, see `help set`.\n", settings[block].value);
return false;
} else if (*end != '\0') {
/* parse failed before end, probably trailing garbage, eg 34=9/16foo */
show_error("trailing garbage after delay count, `%s`.\n", settings[block].value);
return false;
} else if (settings[block].seconds == 0) {
/* 10=24/0 disables continuous mode */
show_info("you specified a zero delay, disabling continuous mode.\n");
} else {
/* valid delay count seen and understood */
show_info("setting %s every %u seconds until interrupted...\n", settings[block].matchids ? settings[block]. matchids : "all", settings[block].seconds);
/* continuous mode on */
cont = true;
}
/* remove any delay suffix from the value */
settings[block].value =
strndupa(settings[block].value,
(size_t) (delay - settings[block].value));
} /* if (strchr('/')) */
} /* for(block...) */
/* --- setup a longjmp to handle interrupt --- */
if (INTERRUPTABLE()) {
/* control returns here when interrupted */
// settings is allocated with alloca, do not free it
// free(settings);
sm_detach(vars->target);
ENDINTERRUPTABLE();
return true;
}
/* --- execute the parsed setting structs --- */
while (true) {
uservalue_t userval;
/* for every settings struct */
for (block = 0; block < argc - 1; block++) {
/* check if this block has anything to do in this iteration */
if (seconds != 1) {
/* not the first iteration (all blocks get executed first iteration) */
/* if settings.seconds is zero, then this block is only executed once */
/* if seconds % settings.seconds is zero, then this block must be executed */
if (settings[block].seconds == 0
|| (seconds % settings[block].seconds) != 0)
continue;
}
/* convert value */
if (!parse_uservalue_number(settings[block].value, &userval)) {
show_error("bad number `%s` provided\n", settings[block].value);
goto fail;
}
/* check if specific match(s) were specified */
if (settings[block].matchids != NULL) {
struct set match_set;
match_location loc;
if (parse_uintset(settings[block].matchids, &match_set, vars->num_matches)
== false) {
show_error("failed to parse the set, try `help set`.\n");
goto fail;
}
foreach_set_fw(i, &match_set) {
loc = nth_match(vars->matches, match_set.buf[i]);
if (loc.swath) {
value_t v;
char *address = remote_address_of_nth_element(loc.swath, loc.index);
v = data_to_val(loc.swath, loc.index);
/* copy userval onto v */
/* XXX: valcmp? make sure the sizes match */
uservalue2value(&v, &userval);
show_info("setting *%p to %#"PRIx64"...\n", address, v.int64_value);
/* set the value specified */
fix_endianness(&v, vars->options.reverse_endianness);
if (sm_setaddr(vars->target, address, &v) == false) {
show_error("failed to set a value.\n");
set_cleanup(&match_set);
goto fail;
}
} else {
show_error("BUG: set: id <%zu> match failure\n", match_set.buf[i]);
set_cleanup(&match_set);
goto fail;
}
}
set_cleanup(&match_set);
} else {
matches_and_old_values_swath *reading_swath_index = vars->matches->swaths;
size_t reading_iterator = 0;
/* user wants to set all matches */
while (reading_swath_index->first_byte_in_child) {
/* only actual matches are considered */
if (reading_swath_index->data[reading_iterator].match_info != flags_empty)
{
char *address = remote_address_of_nth_element(reading_swath_index, reading_iterator);
value_t v;
v = data_to_val(reading_swath_index, reading_iterator);
/* XXX: as above : make sure the sizes match */
uservalue2value(&v, &userval);
show_info("setting *%p to %#"PRIx64"...\n", address, v.int64_value);
fix_endianness(&v, vars->options.reverse_endianness);
if (sm_setaddr(vars->target, address, &v) == false) {
show_error("failed to set a value.\n");
goto fail;
}
}
/* go on to the next one... */
++reading_iterator;
if (reading_iterator >= reading_swath_index->number_of_bytes)
{
reading_swath_index = (matches_and_old_values_swath *)
local_address_beyond_last_element(reading_swath_index);
reading_iterator = 0;
}
}
} /* if (matchid != NULL) else ... */
} /* for(block) */
if (cont) {
sleep(1);
} else {
break;
}
seconds++;
} /* while(true) */
ENDINTERRUPTABLE();
return true;
fail:
ENDINTERRUPTABLE();
return false;
}
/* Accepts a numerical argument to print up to N matches, defaults to 10k
* FORMAT (don't change, front-end depends on this):
* [#no] addr, value, [possible types (separated by space)]
*/
bool handler__list(globals_t *vars, char **argv, unsigned argc)
{
unsigned long num = 0;
size_t buf_len = 128; /* will be realloc'd later if necessary */
int printed;
element_t *np = NULL;
char *v;
struct winsize w;
FILE *pager;
unsigned long max_to_print = 10000;
if (argc > 1) {
max_to_print = strtoul(argv[1], NULL, 0x00);
if (max_to_print == 0) {
show_error("`%s` is not a valid positive integer.\n", argv[1]);
return false;
}
}
if (vars->num_matches == 0)
return false;
if ((v = malloc(buf_len)) == NULL)
{
show_error("memory allocation failed.\n");
return false;
}
if (vars->regions)
np = vars->regions->head;
matches_and_old_values_swath *reading_swath_index = vars->matches->swaths;
size_t reading_iterator = 0;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
if (!vars->options.backend)
show_warn("handler__list(): couldn't get terminal size.\n");
pager = get_pager(stdout);
} else {
/* check if the output fits in the terminal window */
pager = (w.ws_row >= MIN(max_to_print, vars->num_matches)) ? stdout : get_pager(stdout);
}
/* list all known matches */
while (reading_swath_index->first_byte_in_child) {
if (num == max_to_print) {
if (num < vars->num_matches && !vars->options.backend)
fprintf(pager, "[...]\n");
break;
}
match_flags flags = reading_swath_index->data[reading_iterator].match_info;
/* only actual matches are considered */
if (flags != flags_empty)
{
switch(vars->options.scan_data_type)
{
case BYTEARRAY:
buf_len = flags * 3 + 32;
v = realloc(v, buf_len); /* for each byte and the suffix, this should be enough */
if (v == NULL)
{
show_error("memory allocation failed.\n");
goto fail;
}
printed = bytearray_match_to_text(v, buf_len, reading_swath_index, reading_iterator, flags);
printed += snprintf(v + printed, buf_len - printed, ", [bytearray:%u]", flags);
assert(printed < buf_len);
break;
case STRING:
buf_len = flags + 32; /* for the string and suffix, this should be enough */
v = realloc(v, buf_len);
if (v == NULL)
{
show_error("memory allocation failed.\n");
goto fail;
}
printed = string_match_to_text(v, buf_len, reading_swath_index, reading_iterator, flags);
printed += snprintf(v + printed, buf_len - printed, ", [string:%u]", flags);
assert(printed < buf_len);
break;
default: /* numbers */
; /* cheat gcc */
value_t val = data_to_val(reading_swath_index, reading_iterator);
valtostr(&val, v, buf_len);
break;
}
char *address = remote_address_of_nth_element(reading_swath_index, reading_iterator);
unsigned long address_ul = (unsigned long)address;
unsigned int region_id = 99;
unsigned long match_off = 0;
const char *region_type = "??";
/* get region info belonging to the match -
* note: we assume the regions list and matches are sorted
*/
while (np) {
region_t *region = np->data;
unsigned long region_start = (unsigned long)region->start;
if (address_ul < region_start + region->size &&
address_ul >= region_start) {
region_id = region->id;
match_off = address_ul - region->load_addr;
region_type = region_type_names[region->type];
break;
}
np = np->next;
}
fprintf(pager, "[%2lu] "POINTER_FMT", %2u + "POINTER_FMT", %5s, %s\n",
num++, address_ul, region_id, match_off, region_type, v);
}
/* go on to the next one... */
++reading_iterator;
if (reading_iterator >= reading_swath_index->number_of_bytes)
{
reading_swath_index = (matches_and_old_values_swath *)
local_address_beyond_last_element(reading_swath_index);
reading_iterator = 0;
}
}
free(v);
close_pager(pager);
return true;
fail:
free(v);
close_pager(pager);
return false;
}
bool handler__delete(globals_t * vars, char **argv, unsigned argc)
{
struct set del_set;
if (argc != 2) {
show_error("was expecting one argument, see `help delete`.\n");
return false;
}
if (vars->num_matches == 0) {
show_error("nothing to delete.\n");
return false;
}
if (!parse_uintset(argv[1], &del_set, (size_t)vars->num_matches)) {
show_error("failed to parse the set, try `help delete`.\n");
return false;
}
size_t match_counter = 0;
size_t set_idx = 0;
matches_and_old_values_swath *reading_swath_index = vars->matches->swaths;
size_t reading_iterator = 0;
while (reading_swath_index->first_byte_in_child) {
/* only actual matches are considered */
if (reading_swath_index->data[reading_iterator].match_info != flags_empty) {
if (match_counter++ == del_set.buf[set_idx]) {
/* It is not reasonable to check if the matches array can be
* downsized after the deletion.
* So just zero its flags, to mark it as not a REAL match */
reading_swath_index->data[reading_iterator].match_info = flags_empty;
vars->num_matches--;
if (set_idx++ == del_set.size - 1) {
set_cleanup(&del_set);
return true;
}
}
}
/* go on to the next one... */
++reading_iterator;
if (reading_iterator >= reading_swath_index->number_of_bytes) {
reading_swath_index = (matches_and_old_values_swath *)
local_address_beyond_last_element(reading_swath_index);
reading_iterator = 0;
}
}
show_error("BUG: delete: id <%zu> match failure\n", del_set.buf[set_idx]);
set_cleanup(&del_set);
return false;
}
bool handler__reset(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
/* reset scan progress */
vars->scan_progress = 0;
if (vars->matches) { free(vars->matches); vars->matches = NULL; vars->num_matches = 0; }
/* refresh list of regions */
l_destroy(vars->regions);
/* create a new linked list of regions */
if ((vars->regions = l_init()) == NULL) {
show_error("sorry, there was a problem allocating memory.\n");
return false;
}
/* read in maps if a pid is known */
if (vars->target && sm_readmaps(vars->target, vars->regions, vars->options.region_scan_level) != true) {
show_error("sorry, there was a problem getting a list of regions to search.\n");
show_warn("the pid may be invalid, or you don't have permission.\n");
vars->target = 0;
return false;
}
return true;
}
bool handler__pid(globals_t * vars, char **argv, unsigned argc)
{
char *resetargv[] = { "reset", NULL };
char *end = NULL;
if (argc == 2) {
vars->target = (pid_t) strtoul(argv[1], &end, 0x00);
if (vars->target == 0) {
show_error("`%s` does not look like a valid pid.\n", argv[1]);
return false;
}
} else if (vars->target) {
/* print the pid of the target program */
show_info("target pid is %u.\n", vars->target);
return true;
} else {
show_info("no target is currently set.\n");
return false;
}
return handler__reset(vars, resetargv, 1);
}
bool handler__snapshot(globals_t *vars, char **argv, unsigned argc)
{
USEPARAMS();
/* check that a pid has been specified */
if (vars->target == 0) {
show_error("no target set, type `help pid`.\n");
return false;
}
/* remove any existing matches */
if (vars->matches) { free(vars->matches); vars->matches = NULL; vars->num_matches = 0; }
if (sm_searchregions(vars, MATCHANY, NULL) != true) {
show_error("failed to save target address space.\n");
return false;
}
return true;
}
/* dregion <region-id set> */
bool handler__dregion(globals_t *vars, char **argv, unsigned argc)
{
struct set reg_set;
/* need an argument */
if (argc < 2) {
show_error("expected an argument, see `help dregion`.\n");
return false;
}
/* check that there is a process known */
if (vars->target == 0) {
show_error("no target specified, see `help pid`\n");
return false;
}
/* check if there are any regions at all */
if (vars->regions->size == 0) {
show_error("no regions are known.\n");
return false;
}
size_t last_region_id = ((region_t*)(vars->regions->tail->data))->id;
if (!parse_uintset(argv[1], ®_set, last_region_id + 1)) {
show_error("failed to parse the set, try `help dregion`.\n");
return false;
}
/* loop for every reg_id in the set */
for (size_t set_idx = 0; set_idx < reg_set.size; set_idx++) {
size_t reg_id = reg_set.buf[set_idx];
/* initialize list pointers */
element_t *np = vars->regions->head;
element_t *pp = NULL;
/* find the correct region node */
while (np) {
region_t *r = np->data;
/* compare the node id to the id the user specified */
if (r->id == reg_id)
break;
pp = np; /* keep track of prev for l_remove() */
np = np->next;
}
/* check if a match was found */
if (np == NULL) {
show_warn("no region matching %u, or already removed.\n", reg_id);
continue;
}
/* check for any affected matches before removing it */
if(vars->num_matches > 0)
{
region_t *reg_to_delete = np->data;
char *start_address = reg_to_delete->start;
char *end_address = reg_to_delete->start + reg_to_delete->size;
vars->matches = delete_in_address_range(vars->matches, &vars->num_matches,
start_address, end_address);
if (vars->matches == NULL)
{
show_error("memory allocation error while deleting matches\n");
}
}
l_remove(vars->regions, pp, NULL);
}
return true;
}
bool handler__lregions(globals_t * vars, char **argv, unsigned argc)
{
element_t *np = vars->regions->head;
USEPARAMS();
if (vars->target == 0) {
show_error("no target has been specified, see `help pid`.\n");
return false;
}
if (vars->regions->size == 0) {
show_info("no regions are known.\n");
}
/* print a list of regions that have been searched */
while (np) {
region_t *region = np->data;
fprintf(stderr, "[%2u] "POINTER_FMT", %7lu bytes, %5s, "POINTER_FMT", %c%c%c, %s\n",
region->id,
(unsigned long)region->start, region->size,
region_type_names[region->type], region->load_addr,
region->flags.read ? 'r' : '-',
region->flags.write ? 'w' : '-',
region->flags.exec ? 'x' : '-',
region->filename[0] ? region->filename : "unassociated");
np = np->next;
}
return true;
}
/* handles every scan that starts with an operator */
bool handler__operators(globals_t * vars, char **argv, unsigned argc)
{
uservalue_t val;
scan_match_type_t m;
if (argc == 1)
{
zero_uservalue(&val);
}
else if (argc > 2)
{
show_error("too many values specified, see `help %s`", argv[0]);
return false;
}
else
{
if (!parse_uservalue_number(argv[1], &val)) {
show_error("bad value specified, see `help %s`", argv[0]);
return false;
}
}
if (strcmp(argv[0], "=") == 0)
{
m = (argc == 1) ? MATCHNOTCHANGED : MATCHEQUALTO;
}
else if (strcmp(argv[0], "!=") == 0)
{
m = (argc == 1) ? MATCHCHANGED : MATCHNOTEQUALTO;
}
else if (strcmp(argv[0], "<") == 0)
{
m = (argc == 1) ? MATCHDECREASED : MATCHLESSTHAN;
}
else if (strcmp(argv[0], ">") == 0)
{
m = (argc == 1) ? MATCHINCREASED : MATCHGREATERTHAN;
}
else if (strcmp(argv[0], "+") == 0)
{
m = (argc == 1) ? MATCHINCREASED : MATCHINCREASEDBY;
}
else if (strcmp(argv[0], "-") == 0)
{
m = (argc == 1) ? MATCHDECREASED : MATCHDECREASEDBY;
}
else
{
show_error("unrecognized operator seen at handler_operators: \"%s\".\n", argv[0]);
return false;
}
if (vars->matches) {
if (vars->num_matches == 0) {
show_error("there are currently no matches.\n");
return false;
}
if (sm_checkmatches(vars, m, &val) == false) {
show_error("failed to search target address space.\n");
return false;
}
} else {
/* Cannot be used on first scan:
* =, !=, <, >, +, + N, -, - N
* Can be used on first scan:
* = N, != N, < N, > N
*/
if (m == MATCHNOTCHANGED ||
m == MATCHCHANGED ||
m == MATCHDECREASED ||
m == MATCHINCREASED ||
m == MATCHDECREASEDBY ||
m == MATCHINCREASEDBY )
{
show_error("cannot use that search without matches\n");
return false;
}
else
{
if (sm_searchregions(vars, m, &val) != true) {
show_error("failed to search target address space.\n");
return false;
}
}
}
if (vars->num_matches == 1) {
show_info("match identified, use \"set\" to modify value.\n");
show_info("enter \"help\" for other commands.\n");
}
return true;
}
bool handler__version(globals_t *vars, char **argv, unsigned argc)
{
USEPARAMS();
vars->printversion(stderr);
return true;
}
bool handler__string(globals_t * vars, char **argv, unsigned argc)
{
USEPARAMS();
/* Test scan_data_type. Automatically set to string for convenience */
if (vars->options.scan_data_type != STRING)
{
show_info("scan_data_type was not string, it was set automatically.\n");
vars->options.scan_data_type = STRING;
}
/* test the length */
size_t cmdline_length = strlen(vars->current_cmdline);
if (cmdline_length < 3) /* cmdline too short */
{
show_error("please specify a string\n");
return false;
}
size_t string_length = cmdline_length-2;
if (string_length > (uint16_t)(-1)) /* string too long */
{
show_error("String length is limited to %u\n", (uint16_t)(-1));
return false;
}
/* Allocate a copy of the target string. While it would be possible to reuse
* the incoming string, truncating the first 2 chars means it is aligned
* at most at a 2 bytes boundary, which will generate unaligned accesses
* when the string will be read as a sequence of int64 during a scan.
* `malloc()` instead ensures enough alignment for any type.
*/
char *string_value = malloc((string_length+1)*sizeof(char));
if (string_value == NULL)
{
show_error("memory allocation for string failed.\n");
return false;
}
strcpy(string_value, vars->current_cmdline+2);
uservalue_t val;
val.string_value = string_value;
val.flags = string_length;
/* need a pid for the rest of this to work */
if (vars->target == 0) {
goto fail;
}
/* user has specified an exact value of the variable to find */
if (vars->matches) {
if (vars->num_matches == 0) {
show_error("there are currently no matches.\n");
return false;
}
/* already know some matches */
if (sm_checkmatches(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
goto fail;
}
} else {
/* initial search */
if (sm_searchregions(vars, MATCHEQUALTO, &val) != true) {
show_error("failed to search target address space.\n");
goto fail;
}
}
/* check if we now know the only possible candidate */
if (vars->num_matches == 1) {
show_info("match identified, use \"set\" to modify value.\n");
show_info("enter \"help\" for other commands.\n");
}
free(string_value);
return true;
fail:
free(string_value);
return false;
}
static inline bool parse_uservalue_default(const char *str, uservalue_t *val)
{
bool ret = true;
if (!parse_uservalue_number(str, val)) {
show_error("unable to parse number `%s`\n", str);
ret = false;
}
return ret;
}
bool handler__default(globals_t * vars, char **argv, unsigned argc)
{