~ubuntu-branches/ubuntu/wily/keyutils/wily

« back to all changes in this revision

Viewing changes to keyutils.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-10-16 11:45:19 UTC
  • mfrom: (1.1.7) (10.2.16 sid)
  • Revision ID: package-import@ubuntu.com-20141016114519-cqt8zwrg92c31m31
Tags: 1.5.9-5ubuntu1
Disable the tests, hanging on the distro buildd kernels (12.04 LTS).
Verified that the tests succeed with the 14.04 LTS kernels.
Addresses: LP: #1381973.

Show diffs side-by-side

added added

removed removed

Lines of Context:
224
224
        return ret;
225
225
}
226
226
 
227
 
 
228
 
/*****************************************************************************/
229
 
/*
230
 
 * plain realloc is just crazy
231
 
 */
232
 
static void* __xrealloc(void* ptr, size_t size)
233
 
{
234
 
    void* ret;
235
 
 
236
 
    ret = realloc(ptr, size);
237
 
    if(!ret) {
238
 
        free(ptr);
239
 
        return 0;
240
 
    }
241
 
    return ret;
242
 
}
243
 
 
244
 
 
245
227
long keyctl_invalidate(key_serial_t id)
246
228
{
247
229
        return keyctl(KEYCTL_INVALIDATE, id);
248
230
}
249
231
 
 
232
long keyctl_get_persistent(uid_t uid, key_serial_t id)
 
233
{
 
234
        return keyctl(KEYCTL_GET_PERSISTENT, uid, id);
 
235
}
 
236
 
250
237
/*****************************************************************************/
251
238
/*
252
239
 * fetch key description into an allocated buffer
262
249
        if (ret < 0)
263
250
                return -1;
264
251
 
265
 
        buflen = ret;
266
 
        buf = malloc(buflen);
267
 
        if (!buf)
268
 
                return -1;
269
 
 
270
252
        for (;;) {
 
253
                buflen = ret;
 
254
                buf = malloc(buflen);
 
255
                if (!buf)
 
256
                        return -1;
 
257
 
271
258
                ret = keyctl_describe(id, buf, buflen);
272
259
                if (ret < 0) {
273
 
            free(buf);
 
260
                        free(buf);
274
261
                        return -1;
275
 
        }
 
262
                }
276
263
 
277
264
                if (buflen >= ret)
278
265
                        break;
279
 
 
280
 
                buflen = ret;
281
 
                buf = __xrealloc(buf, buflen);
282
 
                if (!buf)
283
 
                        return -1;
 
266
                free(buf);
284
267
        }
285
268
 
286
269
        *_buffer = buf;
287
 
        return buflen - 1;
288
 
 
289
 
} /* end keyctl_describe_alloc() */
 
270
        return ret - 1;
 
271
}
290
272
 
291
273
/*****************************************************************************/
292
274
/*
296
278
 */
297
279
int keyctl_read_alloc(key_serial_t id, void **_buffer)
298
280
{
299
 
        void *buf;
 
281
        char *buf;
300
282
        long buflen, ret;
301
283
 
302
284
        ret = keyctl_read(id, NULL, 0);
303
285
        if (ret < 0)
304
286
                return -1;
305
287
 
306
 
        buflen = ret;
307
 
        buf = malloc(buflen + 1);
308
 
        if (!buf)
309
 
                return -1;
310
 
 
311
288
        for (;;) {
 
289
                buflen = ret;
 
290
                buf = malloc(buflen + 1);
 
291
                if (!buf)
 
292
                        return -1;
 
293
 
312
294
                ret = keyctl_read(id, buf, buflen);
313
295
                if (ret < 0) {
314
 
            free(buf);
 
296
                        free(buf);
315
297
                        return -1;
316
 
        }
 
298
                }
317
299
 
318
300
                if (buflen >= ret)
319
301
                        break;
320
 
 
321
 
                buflen = ret;
322
 
                buf = __xrealloc(buf, buflen + 1);
323
 
                if (!buf)
324
 
                        return -1;
 
302
                free(buf);
325
303
        }
326
304
 
327
 
        ((unsigned char *) buf)[buflen] = 0;
 
305
        buf[ret] = 0;
328
306
        *_buffer = buf;
329
 
        return buflen;
330
 
 
331
 
} /* end keyctl_read_alloc() */
 
307
        return ret;
 
308
}
332
309
 
333
310
/*****************************************************************************/
334
311
/*
345
322
        if (ret < 0)
346
323
                return -1;
347
324
 
348
 
        buflen = ret;
349
 
        buf = malloc(buflen);
350
 
        if (!buf)
351
 
                return -1;
352
 
 
353
325
        for (;;) {
 
326
                buflen = ret;
 
327
                buf = malloc(buflen);
 
328
                if (!buf)
 
329
                        return -1;
 
330
 
354
331
                ret = keyctl_get_security(id, buf, buflen);
355
 
                if (ret < 0)
 
332
                if (ret < 0) {
 
333
                        free(buf);
356
334
                        return -1;
 
335
                }
357
336
 
358
337
                if (buflen >= ret)
359
338
                        break;
360
 
 
361
 
                buflen = ret;
362
 
                buf = realloc(buf, buflen);
363
 
                if (!buf)
364
 
                        return -1;
 
339
                free(buf);
365
340
        }
366
341
 
367
342
        *_buffer = buf;
368
 
        return buflen - 1;
 
343
        return ret - 1;
369
344
}
370
345
 
371
346
/*
451
426
        return 0;
452
427
}
453
428
 
 
429
/*
 
430
 * Find a key by type and description
 
431
 */
 
432
key_serial_t find_key_by_type_and_desc(const char *type, const char *desc,
 
433
                                       key_serial_t destringid)
 
434
{
 
435
        key_serial_t id, error;
 
436
        FILE *f;
 
437
        char buf[1024], typebuf[40], rdesc[1024], *kdesc, *cp;
 
438
        int n, ndesc, dlen;
 
439
 
 
440
        error = ENOKEY;
 
441
 
 
442
        id = request_key(type, desc, NULL, destringid);
 
443
        if (id >= 0 || errno == ENOMEM)
 
444
                return id;
 
445
        if (errno != ENOKEY)
 
446
                error = errno;
 
447
 
 
448
        dlen = strlen(desc);
 
449
 
 
450
        f = fopen("/proc/keys", "r");
 
451
        if (!f) {
 
452
                fprintf(stderr, "libkeyutils: Can't open /proc/keys: %m\n");
 
453
                return -1;
 
454
        }
 
455
 
 
456
        while (fgets(buf, sizeof(buf), f)) {
 
457
                cp = strchr(buf, '\n');
 
458
                if (*cp)
 
459
                        *cp = '\0';
 
460
 
 
461
                ndesc = 0;
 
462
                n = sscanf(buf, "%x %*s %*u %*s %*x %*d %*d %s %n",
 
463
                           &id, typebuf, &ndesc);
 
464
                if (n == 2 && ndesc > 0 && ndesc <= cp - buf) {
 
465
                        if (strcmp(typebuf, type) != 0)
 
466
                                continue;
 
467
                        kdesc = buf + ndesc;
 
468
                        if (memcmp(kdesc, desc, dlen) != 0)
 
469
                                continue;
 
470
                        if (kdesc[dlen] != ':' &&
 
471
                            kdesc[dlen] != '\0' &&
 
472
                            kdesc[dlen] != ' ')
 
473
                                continue;
 
474
                        kdesc[dlen] = '\0';
 
475
 
 
476
                        /* The key type appends extra stuff to the end of the
 
477
                         * description after a colon in /proc/keys.  Colons,
 
478
                         * however, are allowed in descriptions, so we need to
 
479
                         * make a further check. */
 
480
                        n = keyctl_describe(id, rdesc, sizeof(rdesc) - 1);
 
481
                        if (n == -1) {
 
482
                                if (errno != ENOKEY)
 
483
                                        error = errno;
 
484
                                if (errno == ENOMEM)
 
485
                                        break;
 
486
                        }
 
487
                        if (n >= sizeof(rdesc) - 1)
 
488
                                continue;
 
489
                        rdesc[n] = '\0';
 
490
 
 
491
                        cp = strrchr(rdesc, ';');
 
492
                        if (!cp)
 
493
                                continue;
 
494
                        cp++;
 
495
                        if (strcmp(cp, desc) != 0)
 
496
                                continue;
 
497
 
 
498
                        fclose(f);
 
499
 
 
500
                        if (destringid &&
 
501
                            keyctl_link(id, destringid) == -1)
 
502
                                return -1;
 
503
 
 
504
                        return id;
 
505
                }
 
506
        }
 
507
 
 
508
        fclose(f);
 
509
        errno = error;
 
510
        return -1;
 
511
}
 
512
 
454
513
#ifdef NO_GLIBC_KEYERR
455
514
/*****************************************************************************/
456
515
/*