summaryrefslogtreecommitdiff
path: root/libportseal/portseal.c
blob: f9523c61dc52ce126124953d8ce1b8c70abe6206 (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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <error.h>
#include <execinfo.h>
#include <fcntl.h>
#include <hurd.h>
#include <mach.h>
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

#include <urcu.h>		/* RCU flavor */
#include <urcu/rcuhlist.h>	/* RCU hlist */
#include <urcu/rculfhash.h>	/* RCU Lock-free hash table */
#include <urcu/compiler.h>	/* For CAA_ARRAY_SIZE */
#include "jhash.h"		/* Example hash function */

#include "portseal.h"

#if 0
#  define TRACE(X...) error(0, 0, X)
#else
#  define TRACE(X...)
#endif

#define PORTSEAL_PORTS_SIZE 1000
int ports[PORTSEAL_PORTS_SIZE];

struct object {
  struct cds_lfht_node node;	/* Chaining in hash table */
  struct rcu_head rcu_head;	/* For call_rcu() */
  struct cds_hlist_head ports;
  unsigned long addr;
  size_t size;
};

struct port {
  mach_port_t *p;
  struct cds_hlist_node node;	/* Linked-list chaining */
};

static uint32_t portseal_seed;
static struct cds_lfht *portseal_object_hash;
static int portseal_is_initialized;

/* Reference counting.  */
void
portseal_increment (mach_port_t p)
{
  if (! MACH_PORT_VALID (p))
    return;

  if (p >= PORTSEAL_PORTS_SIZE)
    {
      TRACE ("port name %d >= PORTSEAL_PORTS_SIZE", (int) p);
      return;
    }

  TRACE ("portseal_increment (%d)", (int) p);
  __atomic_add_fetch (&ports[p], 1, __ATOMIC_RELAXED);
}

static char *mach_port_right_str[] =
  {
    "send",
    "receive",
    "send-once",
    "port-set",
    "dead-name",
  };

static void
portseal_dec (mach_port_t p)
{
  if (! MACH_PORT_VALID (p))
    return;

  if (p >= PORTSEAL_PORTS_SIZE)
    {
      TRACE ("port name %d >= PORTSEAL_PORTS_SIZE", (int) p);
      return;
    }

  int c = __atomic_sub_fetch (&ports[p], 1, __ATOMIC_RELAXED);
  TRACE ("portseal_decrement (%d) to %d", (int) p, c);
  if (c > 0)
    return;

  /* check hurd ports */
  for (int i = 0; i < INIT_PORT_MAX; i++)
    if (HURD_PORT_USE (&_hurd_ports[i], port == p))
      /* the libc has a reference, it's fine */
      return;

  error_t err = 0;
  mach_port_urefs_t refs;
  mach_port_right_t typ;

  for (typ = MACH_PORT_RIGHT_SEND;
       typ < MACH_PORT_RIGHT_NUMBER;
       typ++)
    {
      err = mach_port_get_refs (mach_task_self (),
                                p,
                                typ,
                                &refs);
      if (err)
        goto out;
      if (refs)
        goto leak;
    }

 out:
  if (err && err != KERN_INVALID_NAME)
    error (0, err, "error in portseal_check_leak");
  return;

 leak:
  error (0, 0, "leaked %s right %d", mach_port_right_str[typ], (int) p);
  void *array[20];
  size_t size;
  size = backtrace (array, 20);
  backtrace_symbols_fd (&array[2], size - 2, 2);
}

/* Used as cleanup function when variables with storage class auto,
   i.e. variables residing on the stack.  */
void
portseal_decrement_p (mach_port_t *p)
{
  portseal_dec (*p);
}

void
portseal_decrement (mach_port_t p)
{
  portseal_dec (p);
}

/* Used to wrap port assignments.  */
mach_port_t
portseal_set_port (mach_port_t *p, mach_port_t new)
{
  portseal_dec (*p);

  if (! MACH_PORT_VALID (new))
    goto out;

  portseal_increment (new);

  struct object *o = NULL, *node;
  struct cds_lfht_iter iter;
  rcu_read_lock();
  cds_lfht_for_each_entry (portseal_object_hash, &iter, node, node)
    if ((unsigned long) node->addr <= (unsigned long) p
        && (unsigned long) p < (unsigned long) node->addr + node->size)
      {
        o = node;
        break;
      }
  rcu_read_unlock();

  if (o != NULL) {
    TRACE ("set port in object %p", (void *) o->addr);
    struct port *port = malloc (sizeof *port);
    if (port) {
      port->p = p;
      cds_hlist_add_head_rcu (&port->node, &o->ports);
    }
  }

 out:
  return new;
}

/* Memory management.  */
void *
portseal_malloc (size_t size)
{
  struct object *o;
  posix_memalign ((void **) &o, 8, size + sizeof (struct object));
  if (! o)
    return NULL;

  void *p = o + sizeof (struct object);

  cds_lfht_node_init (&o->node);
  CDS_INIT_HLIST_HEAD (&o->ports);
  o->addr = (unsigned long) p;
  o->size = size;
  unsigned long hash = jhash (&p, sizeof p, portseal_seed);
  rcu_read_lock ();
  cds_lfht_add (portseal_object_hash, hash, &o->node);
  rcu_read_unlock ();

  return p;
}

static void
free_object (struct rcu_head *head)
{
  struct object *node = caa_container_of (head, struct object, rcu_head);
  TRACE ("free_object(%p)", node);
  free (node);
}

static int
match (struct cds_lfht_node *ht_node, const void *_key)
{
  struct object *o =
    caa_container_of(ht_node, struct object, node);
  const unsigned long *key = _key;

  return *key == o->addr;
}

void
portseal_free (void *p)
{
  TRACE ("free(%p)", p);

  unsigned long hash = jhash (&p, sizeof p, portseal_seed);
  rcu_read_lock ();
  struct cds_lfht_iter iter;
  cds_lfht_lookup (portseal_object_hash, hash, match, &p, &iter);
  struct cds_lfht_node *ht_node = cds_lfht_iter_get_node (&iter);
  if (ht_node)
    {
      if (! cds_lfht_del (portseal_object_hash, ht_node))
        {
          struct object *del_node =
            caa_container_of (ht_node,
                              struct object, node);

          struct port *port;
          struct cds_hlist_node *pos;
          cds_hlist_for_each_entry (port, pos, &del_node->ports, node) {
            portseal_decrement (*port->p);
            *port->p = MACH_PORT_NULL;
            free (port);
          }

          call_rcu (&del_node->rcu_head, free_object);
        }
    }
  rcu_read_unlock ();
}

/* Initialization and teardown.  */

static void __attribute__ ((constructor))
portseal_init ()
{
  portseal_object_hash = cds_lfht_new(1, 1, 0,
                                      CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING,
                                      NULL);
  if (! portseal_object_hash)
    error (1, errno, "Error allocating hash table");

  portseal_seed = (uint32_t) time (NULL);
  rcu_register_thread ();

  portseal_is_initialized = 1;
}

static void __attribute__ ((destructor))
portseal_destroy () {
  rcu_unregister_thread ();
}