summaryrefslogtreecommitdiff
path: root/utils/rpcscan.c
blob: 3ddf791ad3bcfb6cdb98ca175c9854a88ccb5aa3 (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
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
/* An RPC scanner for the Hurd.

   Copyright (C) 2015 Free Software Foundation, Inc.

   This file is part of the GNU Hurd.

   The GNU Hurd 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, or (at
   your option) any later version.

   The GNU Hurd 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.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include <mach.h>
#include <hurd.h>
#include <hurd/ihash.h>
#include <mach/message.h>
#include <unistd.h>
#include <argp.h>
#include <error.h>
#include <string.h>
#include <version.h>
#include <sys/wait.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stddef.h>
#include <argz.h>

#include "msgids.h"

/* Send messages with arbitrary message ids.  */
struct Request {
  mach_msg_header_t Head;
} RequestTemplate;

struct Reply {
  mach_msg_header_t Head;
  mach_msg_type_t RetCodeType;
  kern_return_t RetCode;
  char Body[4096];
};

union {
  struct Request Request;
  struct Reply Reply;
} Message;

error_t
setup (mach_port_t request, mach_msg_type_name_t requestType)
{
  RequestTemplate.Head.msgh_remote_port = request;
  if (! MACH_PORT_VALID (RequestTemplate.Head.msgh_local_port))
    RequestTemplate.Head.msgh_local_port = mach_reply_port ();
  RequestTemplate.Head.msgh_bits =
    MACH_MSGH_BITS (requestType, MACH_MSG_TYPE_MAKE_SEND_ONCE);
  return 0;
}

error_t
send (mach_msg_id_t msgid)
{
  error_t err;
  Message.Request = RequestTemplate;
  Message.Request.Head.msgh_id = msgid;
  err = mach_msg (&Message.Request.Head,
		  MACH_SEND_MSG|MACH_RCV_MSG|MACH_MSG_OPTION_NONE,
		  sizeof Message.Request,
		  sizeof Message.Reply,
		  Message.Request.Head.msgh_local_port,
		  MACH_MSG_TIMEOUT_NONE,
		  MACH_PORT_NULL);
  if (err)
    return err;

  /* XXX typecheck */
  return Message.Reply.RetCode;
}

typedef error_t (*setup_function_t) ();
setup_function_t setup_target;
void *setup_argument;

error_t
setup_task_target (void)
{
  error_t err;
  static task_t task;
  static mach_msg_type_name_t taskType = MACH_MSG_TYPE_COPY_SEND;

  if (MACH_PORT_VALID (task))
    {
      task_terminate (task);
      mach_port_deallocate (mach_task_self (), task);
    }

  err = task_create (mach_task_self (), 0, &task);
  if (err)
    return err;

  return setup (task, taskType);
}

error_t
setup_thread_target (void)
{
  error_t err;
  static task_t task;
  static thread_t thread;

  if (MACH_PORT_VALID (thread))
    {
      thread_terminate (thread);
      mach_port_deallocate (mach_task_self (), thread);
    }

  if (MACH_PORT_VALID (task))
    {
      task_terminate (task);
      mach_port_deallocate (mach_task_self (), task);
    }

  err = task_create (mach_task_self (), 0, &task);
  if (err)
    return err;

  err = thread_create (task, &thread);
  if (err)
    return err;

  return setup (thread, MACH_MSG_TYPE_COPY_SEND);
}

error_t
setup_proc_target (void)
{
  error_t err;
  static task_t task;
  static process_t proc, target;
  static mach_msg_type_name_t targetType = MACH_MSG_TYPE_COPY_SEND;

  if (! MACH_PORT_VALID (proc))
    proc = getproc ();
  if (MACH_PORT_VALID (task))
    mach_port_deallocate (mach_task_self (), task);
  if (MACH_PORT_VALID (target))
    mach_port_deallocate (mach_task_self (), target);

  err = task_create (mach_task_self (), 0, &task);
  if (err)
    return err;

  err = proc_task2proc (proc, task, &target);
  if (err)
    return err;

  return setup (target, targetType);
}

error_t
setup_auth_target (void)
{
  static auth_t auth;
  static mach_msg_type_name_t authType = MACH_MSG_TYPE_COPY_SEND;

  if (MACH_PORT_VALID (auth))
    mach_port_deallocate (mach_task_self (), auth);

  auth = getauth ();
  if (! MACH_PORT_VALID (auth))
    return errno;

  return setup (auth, authType);
}

error_t
setup_hurd_target (void)
{
  char *name = (char *) setup_argument;
  mach_port_t request;
  mach_msg_type_name_t requestType;

  request = file_name_lookup (name, 0, 0);
  if (! MACH_PORT_VALID (request))
    return errno;
  requestType = MACH_MSG_TYPE_COPY_SEND;

  return setup (request, requestType);
}

task_t extract_target_task;
mach_port_t extract_target_port;
mach_msg_type_name_t extract_target_type;

error_t
setup_extract_target (void)
{
  error_t err;
  mach_port_t request;
  mach_msg_type_name_t requestType;

  err = mach_port_extract_right (extract_target_task,
                                 extract_target_port,
                                 extract_target_type,
                                 &request,
                                 &requestType);
  if (err)
    error (1, err, "mach_port_extract_right");
  if (err)
    return err;
  requestType = MACH_MSG_TYPE_COPY_SEND;
  return setup (request, requestType);
}

const char *argp_program_version = STANDARD_HURD_VERSION (rpcscan);

char **cmd_argv;
int verbose;
int numeric;
int subsystem;

#define OPT_TARGET_TASK		-1
#define OPT_TARGET_THREAD	-2
#define OPT_TARGET_PROC		-3
#define OPT_TARGET_AUTH		-4
#define OPT_TARGET_EXTRACT	-5

static const struct argp_option options[] =
{
  {NULL, 0, NULL, OPTION_DOC, "Target selection", 1},
  {"task", OPT_TARGET_TASK, NULL, 0, "target a task port", 1},
  {"thread", OPT_TARGET_THREAD, NULL, 0, "target a thread port", 1},
  {"proc", OPT_TARGET_PROC, NULL, 0, "target a proc port", 1},
  {"auth", OPT_TARGET_AUTH, NULL, 0, "target an auth port", 1},
  {"extract", OPT_TARGET_EXTRACT, "PID.PORT", 0, "target port PORT of PID", 1},

  {NULL, 0, NULL, OPTION_DOC, "Options", 2},
  {"verbose", 'v', NULL, 0, "be verbose", 2},
  {"numeric", 'n', NULL, 0, "show numeric message ids", 2},
  {"subsystem", 's', NULL, 0, "show subsystem names", 2},
  {0}
};

static const char args_doc[] = "[FILE]";
static const char doc[] = "Scan a given Mach port.";

/* Parse our options...	 */
error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
  error_t err;
  switch (key)
    {
    case 'v':
      verbose = 1;
      break;

    case 'n':
      numeric = 1;
      break;

    case 's':
      subsystem = 1;
      break;

#define SELECT_TARGET(target)					\
      if (setup_target)						\
	argp_error (state, "Multiple targets specified.");	\
      setup_target = target;

    case OPT_TARGET_TASK:
      SELECT_TARGET (setup_task_target);
      break;

    case OPT_TARGET_THREAD:
      SELECT_TARGET (setup_thread_target);
      break;

    case OPT_TARGET_PROC:
      SELECT_TARGET (setup_proc_target);
      break;

    case OPT_TARGET_AUTH:
      SELECT_TARGET (setup_auth_target);
      break;

    case OPT_TARGET_EXTRACT:;
      process_t proc;
      pid_t pid;
      char *end;

      pid = strtol (arg, &end, 10);
      if (arg == end || *end != '.')
        argp_error (state, "Expected format PID.PORT, got `%s'.", arg);

      arg = end + 1;
      extract_target_port = strtol (arg, &end, 10);
      if (arg == end || *end != '\0')
        argp_error (state, "Expected format PORT, got `%s'.", arg);

      proc = getproc ();
      err = proc_pid2task (proc, pid, &extract_target_task);
      if (err)
        argp_failure (state, 1, err,
                      "Could not get task of process %d", pid);

      extract_target_type = MACH_MSG_TYPE_COPY_SEND; /* XXX */
      SELECT_TARGET (setup_extract_target);
      break;

    case ARGP_KEY_ARG:
      SELECT_TARGET (setup_hurd_target);
      setup_argument = arg;
      break;
#undef SELECT_TARGET

    case ARGP_KEY_NO_ARGS:
      if (setup_target == NULL)
	argp_usage (state);
      break;

    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

const struct argp_child children[] =
  {
    { .argp=&msgid_argp, },
    { 0 }
  };

const struct argp argp = { options, parse_opt, args_doc, doc, children };

void
format_msgid (char *buf, size_t len, mach_msg_id_t id)
{
  if (numeric)
  numeric:
    snprintf (buf, len, "%d", id);
  else
    {
      const struct msgid_info *info;
      info = msgid_info (id);
      if (info == NULL)
        goto numeric;

      if (subsystem)
        snprintf (buf, len, "%s/%s", info->subsystem, info->name);
      else
        snprintf (buf, len, "%s", info->name);
    }
}

int
main (int argc, char **argv)
{
  error_t err;
  mach_msg_id_t msgid;

  /* Parse our arguments.  */
  argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, 0);

  err = setup_target ();
  if (err)
    /* Initial setup failed.  Bail out.  */
    error (1, err, "%s",
	setup_target == setup_hurd_target? (char *) setup_argument: "setup");

  for (msgid = 0; msgid < 500000; msgid++)
    {
      err = send (msgid);
      switch (err)
	{
	case MACH_SEND_INVALID_DEST:
	  err = setup_target ();
	  if (err)
	    error (1, err, "setup");
	  msgid--;	/* redo */
	  continue;

	case MIG_BAD_ID:
	  /* do nothing */
	  break;

	default:;
          char buf[80];
          format_msgid (buf, sizeof buf, msgid);
          if (verbose)
            error (0, err, "%s", buf);
          else
            fprintf (stdout, "%s\n", buf);
	}
    }

  return EXIT_SUCCESS;
}