File: name_service_door.c

package info (click to toggle)
valgrind 1%3A3.24.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 176,332 kB
  • sloc: ansic: 795,029; exp: 26,134; xml: 23,472; asm: 14,393; cpp: 9,397; makefile: 7,464; sh: 6,122; perl: 5,446; python: 1,498; javascript: 981; awk: 166; csh: 1
file content (272 lines) | stat: -rw-r--r-- 8,035 bytes parent folder | download | duplicates (7)
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
/* Tests for name switch cache daemon (nscd) door wrapper. */

#include "config.h"

#include <assert.h>
#include <ctype.h>
#include <door.h>
#include <fcntl.h>
#include <inttypes.h>
#include <nss_dbdefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/mman.h>

#if defined(SOLARIS_NSCD_DOOR_SYSTEM_VOLATILE)
#define DOOR_FILE "/system/volatile/name_service_door"
#else
#define DOOR_FILE "/var/run/name_service_door"
#endif

#define HEADER(file, test_name) \
   fprintf(file, "---------------------------------------------------------\n"  \
                 "%s\n"                                                         \
                 "---------------------------------------------------------\n", \
                 test_name);


static long x0;

/* It's possible that the system allocated a new memory for rbuf.
   Unmap it if it is the case. */
static int handle_rbuf(door_arg_t *params, void *buf)
{
   if (params->rbuf != buf) {
      if (munmap(params->rbuf, params->rsize) != 0) {
         perror("munmap");
         return EINVAL;
      }
   }

   return 0;
}

__attribute__((noinline))
static int test_app_small_request(int did)
{
   /* Set call parameters. */
   size_t buf_size = sizeof(uint32_t);
   char *buf = malloc(buf_size);
   assert(buf != NULL);

   nss_pheader_t *header = (nss_pheader_t *) buf;
   header->nsc_callnumber = x0 + NSCD_GETENT;

   door_arg_t params;
   params.data_ptr = buf;
   params.data_size = buf_size;
   params.desc_ptr = NULL;
   params.desc_num = 0;
   params.rbuf = buf;
   params.rsize = buf_size;

   /* Make the call. */
   if (door_call(did, &params) != 0) {
      return errno;
   }

   return handle_rbuf(&params, buf);
}

__attribute__((noinline))
static int test_app_uninitialized_request(int did)
{
   /* Set call parameters. */
   size_t buf_size = sizeof(nss_pheader_t) + sizeof(nss_dbd_t);
   char *buf = malloc(buf_size);
   assert(buf != NULL);

   nss_pheader_t *header = (nss_pheader_t *) buf;
   header->nsc_callnumber = x0 + NSCD_GETENT;
   header->dbd_off = x0 + sizeof(nss_pheader_t);
   header->dbd_len = x0 + sizeof(nss_dbd_t);
   nss_dbd_t *dbd = (nss_dbd_t *) (buf + sizeof(nss_pheader_t));
   dbd->flags = x0;
   dbd->o_name = x0 + 100;
   dbd->o_config_name = x0 + 100;
   dbd->o_default_config = x0 + 100;
   header->key_off = x0 + sizeof(nss_pheader_t) + sizeof(nss_dbd_t);
   header->key_len = x0 + 1; // one byte past the end of 'buf'
   header->pbufsiz = x0 + 10000000;

   door_arg_t params;
   params.data_ptr = buf;
   params.data_size = buf_size;
   params.desc_ptr = NULL;
   params.desc_num = 0;
   params.rbuf = buf;
   params.rsize = buf_size;

   /* Make the call. */
   if (door_call(did, &params) != 0) {
      return errno;
   }

   /* Check definedness of response attributes ... */
   int x = 0;
   if (header->p_status != NSS_SUCCESS) x = -1; else x = -2;
   if (header->p_herrno != 0) x = -2; else x = -3;
   if (header->key_off != 0) x = -4; else x = -5;
   if (header->key_len != 0) x = -6; else x = -7;
   if (header->data_off != 0) x = -8; else x = -9;
   if (header->data_len != 0) x = -10; else x = -11;
   /* ... and now one which is not defined. */
   if (header->reserved1 != 0) x = -12; else x = -13;

   handle_rbuf(&params, buf);
   return x;
}

__attribute__((noinline))
static int test_app_proto_icmp(int did)
{
   door_arg_t params;
   char buf[16384];

   /* Set call parameters. */
   nss_pheader_t *header = (nss_pheader_t *) buf;
   header->nsc_callnumber = NSCD_SEARCH;
   header->p_ruid = getuid();
   header->p_euid = geteuid();
   header->p_version = NSCD_HEADER_REV;
   header->p_status = 0;
   header->p_errno = 0;
   header->p_herrno = 0;
   header->libpriv = 0;
   header->nss_dbop = NSS_DBOP_PROTOCOLS_BYNAME;

   size_t name_len = strlen(NSS_DBNAM_PROTOCOLS);
   size_t default_config_len = strlen(NSS_FILES_ONLY);
   header->dbd_off = sizeof(nss_pheader_t);
   header->dbd_len = sizeof(nss_dbd_t) + name_len + 1 + default_config_len + 1;
   nss_dbd_t *dbd = (nss_dbd_t *) (buf + sizeof(nss_pheader_t));
   dbd->o_name = sizeof(nss_dbd_t);
   dbd->o_config_name = 0;
   dbd->o_default_config = dbd->o_name + name_len + 1;
   dbd->flags = 0;
   strcpy(buf + header->dbd_off + dbd->o_name, NSS_DBNAM_PROTOCOLS);
   strcpy(buf + header->dbd_off + dbd->o_default_config,
          NSS_DEFCONF_PROTOCOLS);

   name_len = strlen("icmp");
   header->key_off = header->dbd_off + ROUND_UP(header->dbd_len, sizeof(nssuint_t));
   header->key_len = name_len + 1;
   strcpy(buf + header->key_off, "icmp");

   header->data_off = header->key_off + ROUND_UP(header->key_len, sizeof(nssuint_t));
   header->data_len = 0;
   header->pbufsiz = header->data_off + header->data_len;

   params.data_ptr = buf;
   params.data_size = header->pbufsiz;
   params.desc_ptr = NULL;
   params.desc_num = 0;
   params.rbuf = buf;
   params.rsize = sizeof(buf);

   /* Sanity checks on the nss_pheader_t header. */
   assert(header->p_version == NSCD_HEADER_REV);
   assert(header->dbd_off == sizeof(nss_pheader_t));
   assert((params.data_size & 3) == 0);
   assert((header->dbd_off & 3) == 0);
   assert((header->key_off & 3) == 0);
   assert((header->data_off & 3) == 0);
   assert(header->data_off == params.data_size);
   nssuint_t l1 = header->key_off - header-> dbd_off;
   assert(l1 >= header->dbd_len);
   nssuint_t l2 = header->data_off - header->key_off;
   assert(l2 >= header->key_len);
   assert(sizeof(nss_pheader_t) + l1 + l2 == header->data_off);
   assert(header->data_off + header->data_len == header->pbufsiz);

   /* Make the call. */
   if (door_call(did, &params) != 0) {
      return errno;
   }

   /* Print response attributes. */
   HEADER(stdout, "app_proto_icmp");
   printf("status=%u\n", header->p_status);
   printf("errno=%u\n", header->p_errno);
   printf("herrno=%u\n", header->p_herrno);
   printf("bufsiz=%" PRIu64 "\n", header->pbufsiz);
   printf("dbd_off=%" PRIu64 " dbd_len=%" PRIu64 "\n",
          header->dbd_off, header->dbd_len);
   printf("key_off=%" PRIu64 " key_len=%" PRIu64 "\n",
          header->key_off, header->key_len);
   printf("data_off=%" PRIu64 " data_len=%" PRIu64 "\n",
          header->data_off, header->data_len);
   printf("ext_off=%" PRIu64 " ext_len=%" PRIu64 "\n",
          header->ext_off, header->ext_len);
   printf("key=%s\n", buf + header->key_off);

   /* Parse response proto data. */
   char *p = buf + header->data_off;
   char *limit = p + header->data_len;

   while ((p < limit) && isspace(*p))
      p++;
   char *name_start = p;
   while ((p < limit) && !isspace(*p))
      p++; // skip over the name
   name_len = p - name_start;

   while ((p < limit) && isspace(*p))
      p++;
   char *number_start = p;
   do {
      p++; // skip over the proto number
   } while ((p < limit) && !isspace(*p));
   size_t number_len = p - number_start;

   while ((p < limit) && isspace(*p))
      p++;
   char *aliases_start = p;
   while ((p < limit) && !isspace(*p))
      p++; // skip over the aliases
   size_t aliases_len = p - aliases_start;

   printf("data: name=%.*s number=%.*s aliases=%.*s\n",
      (int) name_len, name_start, (int) number_len, number_start,
      (int) aliases_len, aliases_start);

   return handle_rbuf(&params, buf);
}

int main(int argc, const char *argv[])
{
   /* Uninitialised, but we know px[0] is 0x0. */
   long *px = malloc(sizeof(long));
   x0 = px[0];

   int did = open(DOOR_FILE, O_RDONLY);
   if (did < 0) {
      perror("open " DOOR_FILE);
      fprintf(stderr, "Make sure the name service switch daemon (nscd) "
              "is running.\n");
      return 1;
   }

   struct door_info info;
   if (door_info(did, &info) != 0) {
      perror("door_info " DOOR_FILE);
      close(did);
      return 1;
   }

   HEADER(stderr, "app_small_request");
   test_app_small_request(did);

   HEADER(stderr, "app_uninitialized_request");
   test_app_uninitialized_request(did);

   HEADER(stderr, "app_proto_icmp");
   test_app_proto_icmp(did);

   close(did);

   return 0;
}