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
|
/*
* $Id: cap_alloc.c,v 1.1.1.1.4.1 2000/07/11 05:29:14 agmorgan Exp $
*
* Copyright (c) 1997-8 Andrew G Morgan <morgan@linux.kernel.org>
*
* See end of file for Log.
*
* This file deals with allocation and deallocation of internal
* capability sets as specified by POSIX.1e (formerlly, POSIX 6).
*/
#include "libcap.h"
/*
* Obtain a blank set of capabilities
*/
cap_t cap_init(void)
{
__u32 *raw_data;
cap_t result;
raw_data = malloc( sizeof(__u32) + sizeof(*result) );
if (raw_data == NULL) {
_cap_debug("out of memory");
errno = ENOMEM;
return NULL;
}
*raw_data = CAP_T_MAGIC;
result = (cap_t) (raw_data + 1);
memset(result, 0, sizeof(*result));
_libcap_establish_api();
result->features = _libcap_kernel_features;
result->head.version = _libcap_kernel_version;
return result;
}
/*
* This is an internal library function to duplicate a string and
* tag the result as something cap_free can handle.
*/
char *_libcap_strdup(const char *old)
{
__u32 *raw_data;
if (old == NULL) {
errno = EINVAL;
return NULL;
}
raw_data = malloc( sizeof(__u32) + strlen(old) + 1 );
if (raw_data == NULL) {
errno = ENOMEM;
return NULL;
}
*(raw_data++) = CAP_S_MAGIC;
strcpy((char *) raw_data, old);
return ((char *) raw_data);
}
/*
* This function duplicates an internal capability set with
* malloc()'d memory. It is the responsibility of the user to call
* cap_free() to liberate it.
*/
cap_t cap_dup(cap_t cap_d)
{
cap_t result;
if (!good_cap_t(cap_d)) {
_cap_debug("bad argument");
errno = EINVAL;
return NULL;
}
result = cap_init();
if (result == NULL) {
_cap_debug("out of memory");
return NULL;
}
memcpy(result, cap_d, sizeof(*cap_d));
return result;
}
/*
* Scrub and then liberate an internal capability set.
*/
int cap_free(void *data_p)
{
_cap_debug("liberating something");
if ( good_cap_t(data_p) ) {
_cap_debug("liberating a data item");
data_p = -1 + (__u32 *) data_p;
memset(data_p, 0, sizeof(__u32) + sizeof(struct _cap_struct));
free(data_p);
data_p = NULL;
_cap_debug("liberated a data item");
return 0;
}
if ( good_cap_string(data_p) ) {
int length = strlen(data_p) + sizeof(__u32);
_cap_debug("liberating a string");
data_p = -1 + (__u32 *) data_p;
memset(data_p, 0, length);
free(data_p);
data_p = NULL;
_cap_debug("liberated a string");
return 0;
}
_cap_debug("don't recognize what we're supposed to liberate");
errno = EINVAL;
return -1;
}
|