summaryrefslogtreecommitdiff
path: root/proc-region-rw.c
blob: c2a9b133e3643e59dde8ce1aad7e0f232b0b13a0 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
#include <err.h>

struct context {
   void *buf;

   struct {
      size_t offset, len;
      bool has_offset, has_len;

      struct {
         FILE *src;
         size_t size;
      } wm; // write/map

      enum {
         MODE_MAP,
         MODE_WRITE,
         MODE_READ
      } mode;
   } op;

   struct {
      FILE *mem;
      pid_t pid;
   } proc;
};

static void
usage(const char *argv0)
{
   fprintf(stderr"usage: %s pid map file [offset] [len] < regions\n"
                   "       %s pid write file [offset] [len] < regions\n"
                   "       %s pid read [offset] [len] < regions\n"
                   "       regions must be in /proc/<pid>/maps format", argv0, argv0, argv0);
   exit(EXIT_FAILURE);
}

static void
context_init(struct context *ctx, size_t argc, const char *argv[])
{
   if (argc < 3)
      usage(argv[0]);

   size_t arg = 1;
   *ctx = (struct context){0};
   ctx->proc.pid = strtoull(argv[arg++], NULL10);

   {
      bool w = false, r = false, m = false;
      const char *mode = argv[arg++];
      if (!(m = !strcmp(mode, "map")) && !(w = !strcmp(mode, "write")) && !(r = !strcmp(mode, "read")))
         err(EXIT_FAILURE"mode must be write or read");

      ctx->op.mode = (m ? MODE_MAP : (w ? MODE_WRITE : MODE_READ));
   }

   const char *wmname = NULL;
   if (ctx->op.mode == MODE_MAP || ctx->op.mode == MODE_WRITE) {
      if (argc < arg + 1)
         usage(argv[0]);

      wmname = argv[arg++];
   }

   if (argc >= arg + 1) {
      ctx->op.offset = strtoull(argv[arg++], NULL10);
      ctx->op.has_offset = true;
   }

   if (argc >= arg + 1) {
      ctx->op.len = strtoull(argv[arg++], NULL10);
      ctx->op.has_len = true;
   }

   if (wmname && !(ctx->op.wm.src = fopen(wmname, "rb")))
      err(EXIT_FAILURE"fopen(%s)", wmname);

   if (fseek(ctx->op.wm.src, 0SEEK_END) != 0)
      err(EXIT_FAILURE"fseek");

   ctx->op.wm.size = ftell(ctx->op.wm.src);

   char path[128];
   snprintf(path, sizeof(path), "/proc/%u/mem", ctx->proc.pid);
   if (!(ctx->proc.mem = fopen(path, "w+b")))
      err(EXIT_FAILURE"fopen(%s)", path);
}

static void
context_release(struct context *ctx)
{
   if (ctx->op.wm.src)
      fclose(ctx->op.wm.src);

   fclose(ctx->proc.mem);
   free(ctx->buf);
   *ctx = (struct context){0};
}

static void
for_each_line_in_file(FILE *f, void (*cb)(const char *line, void *data), void *data)
{
   char *buffer = NULL;
   size_t step = 1024, allocated = 0, written = 0, read = 0;
   do {
      if (written + read >= allocated && !(buffer = realloc(buffer, (allocated += step) + 1)))
         err(EXIT_FAILURE"realloc");
      buffer[(written += read)] = 0;
      size_t ate = 0;
      for (char *line = buffer, *nl; (nl = strchr(line, '\n')); line = nl + 1) {
         *nl = 0;
         cb(line, data);
         ate += nl + 1 - line;
      }
      memmove(buffer, buffer + ate, (written = written - ate));
   } while ((read = fread(buffer + written, 1, allocated - written, f)));
   free(buffer);
}

static void
region_cb(const char *line, void *data)
{
   struct context *ctx = data;
   unsigned long start, end, region_offset;
   if (sscanf(line, "%lx-%lx %*s %lx", &start, &end, &region_offset) < 3) {
      warnx("failed to parse mapping:\n%s", line);
      return;
   }

   warnx("%s", line);
   start += ctx->op.offset;

   if (start > end) {
      warnx("write offset %lx is out of bounds", start);
      return;
   }

   region_offset = (ctx->op.mode == MODE_MAP ? region_offset : 0);
   const size_t rlen = (ctx->op.has_len ? ctx->op.len : ctx->op.wm.size); // requested write/read
   const size_t len = (rlen > end - start ? end - start : rlen); // actual write/read

   if (!len)
      return;

   if (!(ctx->buf = realloc(ctx->buf, len)))
      err(EXIT_FAILURE"realloc");

   clearerr(ctx->proc.mem);
   if (ctx->op.mode == MODE_MAP || ctx->op.mode == MODE_WRITE) {
      if (fseek(ctx->op.wm.src, region_offset, SEEK_SET) != 0)
         err(EXIT_FAILURE"fseek");

      const size_t rd = fread(ctx->buf, 1, len, ctx->op.wm.src);

      if (fseek(ctx->proc.mem, start, SEEK_SET) != 0)
         err(EXIT_FAILURE"fseek");

      const size_t wd = fwrite(ctx->buf, 1, rd, ctx->proc.mem);

      if (ferror(ctx->proc.mem)) {
         warn("fread(/proc/%u/mem)", ctx->proc.pid);
         return;
      }

      if (ctx->op.mode == MODE_WRITE) {
         if (rlen > wd) {
            warnx("wrote %lu bytes (%lu bytes truncated) to offset 0x%lx", wd, rlen - wd, start);
         } else {
            warnx("wrote %lu bytes to offset 0x%lx", wd, start);
         }
      } else {
         if (rlen > wd) {
            warnx("mapped %lu bytes (%lu bytes truncated) from offset 0x%lx to offset 0x%lx", wd, rlen - wd, region_offset, start);
         } else {
            warnx("mapped %lu bytes from offset 0x%lx to offset 0x%lx", wd, region_offset, start);
         }
      }
   } else {
      if (fseek(ctx->proc.mem, start, SEEK_SET) != 0)
         err(EXIT_FAILURE"fseek");

      const size_t rd = fread(ctx->buf, 1, len, ctx->proc.mem);

      if (ferror(ctx->proc.mem))
         warn("fread(/proc/%u/mem)", ctx->proc.pid);

      if (fwrite(ctx->buf, 1, rd, stdout) != rd)
         err(EXIT_FAILURE"fwrite");
   }
}

int
main(int argc, const char *argv[])
{
   struct context ctx;
   context_init(&ctx, argc, argv);

   if (ptrace(PTRACE_ATTACH, ctx.proc.pid, NULLNULL) == -1L)
      err(EXIT_FAILURE"ptrace(PTRACE_ATTACH, %u, NULL, NULL)", ctx.proc.pid);

   {
      int status;
      if (waitpid(ctx.proc.pid, &status, 0) == -1 || !WIFSTOPPED(status))
         err(EXIT_FAILURE"waitpid");
   }

   for_each_line_in_file(stdin, region_cb, &ctx);
   ptrace(PTRACE_DETACH, ctx.proc.pid, 10);
   context_release(&ctx);
   return EXIT_SUCCESS;
}