Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/realpath.c |
| 3 | * |
| 4 | * Get the canonicalized absolute pathnames. The basis for TOMOYO. |
| 5 | * |
| 6 | * Copyright (C) 2005-2009 NTT DATA CORPORATION |
| 7 | * |
Tetsuo Handa | 39826a1 | 2009-04-08 22:31:28 +0900 | [diff] [blame] | 8 | * Version: 2.2.0 2009/04/01 |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/mount.h> |
| 14 | #include <linux/mnt_namespace.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 15 | #include <linux/fs_struct.h> |
Stephen Hemminger | 024e1a4 | 2009-10-27 19:24:46 -0700 | [diff] [blame] | 16 | #include <linux/hash.h> |
Tetsuo Handa | 67fa488 | 2009-12-09 15:36:04 +0900 | [diff] [blame] | 17 | #include <linux/magic.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 18 | #include <linux/slab.h> |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 19 | #include "common.h" |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * tomoyo_encode: Convert binary string to ascii string. |
| 23 | * |
| 24 | * @buffer: Buffer for ASCII string. |
| 25 | * @buflen: Size of @buffer. |
| 26 | * @str: Binary string. |
| 27 | * |
| 28 | * Returns 0 on success, -ENOMEM otherwise. |
| 29 | */ |
| 30 | int tomoyo_encode(char *buffer, int buflen, const char *str) |
| 31 | { |
| 32 | while (1) { |
| 33 | const unsigned char c = *(unsigned char *) str++; |
| 34 | |
| 35 | if (tomoyo_is_valid(c)) { |
| 36 | if (--buflen <= 0) |
| 37 | break; |
| 38 | *buffer++ = (char) c; |
| 39 | if (c != '\\') |
| 40 | continue; |
| 41 | if (--buflen <= 0) |
| 42 | break; |
| 43 | *buffer++ = (char) c; |
| 44 | continue; |
| 45 | } |
| 46 | if (!c) { |
| 47 | if (--buflen <= 0) |
| 48 | break; |
| 49 | *buffer = '\0'; |
| 50 | return 0; |
| 51 | } |
| 52 | buflen -= 4; |
| 53 | if (buflen <= 0) |
| 54 | break; |
| 55 | *buffer++ = '\\'; |
| 56 | *buffer++ = (c >> 6) + '0'; |
| 57 | *buffer++ = ((c >> 3) & 7) + '0'; |
| 58 | *buffer++ = (c & 7) + '0'; |
| 59 | } |
| 60 | return -ENOMEM; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * tomoyo_realpath_from_path2 - Returns realpath(3) of the given dentry but ignores chroot'ed root. |
| 65 | * |
| 66 | * @path: Pointer to "struct path". |
| 67 | * @newname: Pointer to buffer to return value in. |
| 68 | * @newname_len: Size of @newname. |
| 69 | * |
| 70 | * Returns 0 on success, negative value otherwise. |
| 71 | * |
| 72 | * If dentry is a directory, trailing '/' is appended. |
| 73 | * Characters out of 0x20 < c < 0x7F range are converted to |
| 74 | * \ooo style octal string. |
| 75 | * Character \ is converted to \\ string. |
| 76 | */ |
| 77 | int tomoyo_realpath_from_path2(struct path *path, char *newname, |
| 78 | int newname_len) |
| 79 | { |
| 80 | int error = -ENOMEM; |
| 81 | struct dentry *dentry = path->dentry; |
| 82 | char *sp; |
| 83 | |
| 84 | if (!dentry || !path->mnt || !newname || newname_len <= 2048) |
| 85 | return -EINVAL; |
| 86 | if (dentry->d_op && dentry->d_op->d_dname) { |
| 87 | /* For "socket:[\$]" and "pipe:[\$]". */ |
| 88 | static const int offset = 1536; |
| 89 | sp = dentry->d_op->d_dname(dentry, newname + offset, |
| 90 | newname_len - offset); |
| 91 | } else { |
Al Viro | 37afdc7 | 2010-02-05 01:41:33 -0500 | [diff] [blame] | 92 | struct path ns_root = {.mnt = NULL, .dentry = NULL}; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 93 | |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 94 | spin_lock(&dcache_lock); |
Al Viro | 37afdc7 | 2010-02-05 01:41:33 -0500 | [diff] [blame] | 95 | /* go to whatever namespace root we are under */ |
| 96 | sp = __d_path(path, &ns_root, newname, newname_len); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 97 | spin_unlock(&dcache_lock); |
Eric W. Biederman | a4054b6 | 2009-11-20 09:12:22 -0800 | [diff] [blame] | 98 | /* Prepend "/proc" prefix if using internal proc vfs mount. */ |
Al Viro | 440b3c6 | 2010-02-05 09:37:21 -0500 | [diff] [blame] | 99 | if (!IS_ERR(sp) && (path->mnt->mnt_flags & MNT_INTERNAL) && |
Tetsuo Handa | 67fa488 | 2009-12-09 15:36:04 +0900 | [diff] [blame] | 100 | (path->mnt->mnt_sb->s_magic == PROC_SUPER_MAGIC)) { |
Eric W. Biederman | a4054b6 | 2009-11-20 09:12:22 -0800 | [diff] [blame] | 101 | sp -= 5; |
| 102 | if (sp >= newname) |
| 103 | memcpy(sp, "/proc", 5); |
| 104 | else |
| 105 | sp = ERR_PTR(-ENOMEM); |
| 106 | } |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 107 | } |
| 108 | if (IS_ERR(sp)) |
| 109 | error = PTR_ERR(sp); |
| 110 | else |
| 111 | error = tomoyo_encode(newname, sp - newname, sp); |
| 112 | /* Append trailing '/' if dentry is a directory. */ |
| 113 | if (!error && dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode) |
| 114 | && *newname) { |
| 115 | sp = newname + strlen(newname); |
| 116 | if (*(sp - 1) != '/') { |
| 117 | if (sp < newname + newname_len - 4) { |
| 118 | *sp++ = '/'; |
| 119 | *sp = '\0'; |
| 120 | } else { |
| 121 | error = -ENOMEM; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | if (error) |
| 126 | printk(KERN_WARNING "tomoyo_realpath: Pathname too long.\n"); |
| 127 | return error; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * tomoyo_realpath_from_path - Returns realpath(3) of the given pathname but ignores chroot'ed root. |
| 132 | * |
| 133 | * @path: Pointer to "struct path". |
| 134 | * |
| 135 | * Returns the realpath of the given @path on success, NULL otherwise. |
| 136 | * |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 137 | * These functions use kzalloc(), so the caller must call kfree() |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 138 | * if these functions didn't return NULL. |
| 139 | */ |
| 140 | char *tomoyo_realpath_from_path(struct path *path) |
| 141 | { |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 142 | char *buf = kzalloc(sizeof(struct tomoyo_page_buffer), GFP_KERNEL); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 143 | |
| 144 | BUILD_BUG_ON(sizeof(struct tomoyo_page_buffer) |
| 145 | <= TOMOYO_MAX_PATHNAME_LEN - 1); |
| 146 | if (!buf) |
| 147 | return NULL; |
| 148 | if (tomoyo_realpath_from_path2(path, buf, |
| 149 | TOMOYO_MAX_PATHNAME_LEN - 1) == 0) |
| 150 | return buf; |
Tetsuo Handa | 8e2d39a | 2010-01-26 20:45:27 +0900 | [diff] [blame] | 151 | kfree(buf); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * tomoyo_realpath - Get realpath of a pathname. |
| 157 | * |
| 158 | * @pathname: The pathname to solve. |
| 159 | * |
| 160 | * Returns the realpath of @pathname on success, NULL otherwise. |
| 161 | */ |
| 162 | char *tomoyo_realpath(const char *pathname) |
| 163 | { |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 164 | struct path path; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 165 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 166 | if (pathname && kern_path(pathname, LOOKUP_FOLLOW, &path) == 0) { |
| 167 | char *buf = tomoyo_realpath_from_path(&path); |
| 168 | path_put(&path); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 169 | return buf; |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * tomoyo_realpath_nofollow - Get realpath of a pathname. |
| 176 | * |
| 177 | * @pathname: The pathname to solve. |
| 178 | * |
| 179 | * Returns the realpath of @pathname on success, NULL otherwise. |
| 180 | */ |
| 181 | char *tomoyo_realpath_nofollow(const char *pathname) |
| 182 | { |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 183 | struct path path; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 184 | |
Al Viro | e24977d | 2009-04-02 21:17:03 -0400 | [diff] [blame] | 185 | if (pathname && kern_path(pathname, 0, &path) == 0) { |
| 186 | char *buf = tomoyo_realpath_from_path(&path); |
| 187 | path_put(&path); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 188 | return buf; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | /* Memory allocated for non-string data. */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 194 | static atomic_t tomoyo_policy_memory_size; |
| 195 | /* Quota for holding policy. */ |
| 196 | static unsigned int tomoyo_quota_for_policy; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 197 | |
| 198 | /** |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 199 | * tomoyo_memory_ok - Check memory quota. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 200 | * |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 201 | * @ptr: Pointer to allocated memory. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 202 | * |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 203 | * Returns true on success, false otherwise. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 204 | * |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 205 | * Caller holds tomoyo_policy_lock. |
| 206 | * Memory pointed by @ptr will be zeroed on success. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 207 | */ |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 208 | bool tomoyo_memory_ok(void *ptr) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 209 | { |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 210 | int allocated_len = ptr ? ksize(ptr) : 0; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 211 | atomic_add(allocated_len, &tomoyo_policy_memory_size); |
| 212 | if (ptr && (!tomoyo_quota_for_policy || |
| 213 | atomic_read(&tomoyo_policy_memory_size) |
| 214 | <= tomoyo_quota_for_policy)) { |
Tetsuo Handa | cd7bec6 | 2010-01-05 06:39:37 +0900 | [diff] [blame] | 215 | memset(ptr, 0, allocated_len); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 216 | return true; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 217 | } |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 218 | printk(KERN_WARNING "ERROR: Out of memory " |
| 219 | "for tomoyo_alloc_element().\n"); |
| 220 | if (!tomoyo_policy_loaded) |
| 221 | panic("MAC Initialization failed.\n"); |
| 222 | return false; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 223 | } |
| 224 | |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 225 | /** |
| 226 | * tomoyo_memory_free - Free memory for elements. |
| 227 | * |
| 228 | * @ptr: Pointer to allocated memory. |
| 229 | */ |
| 230 | void tomoyo_memory_free(void *ptr) |
| 231 | { |
| 232 | atomic_sub(ksize(ptr), &tomoyo_policy_memory_size); |
| 233 | kfree(ptr); |
| 234 | } |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 235 | |
| 236 | /* |
Tetsuo Handa | c3fa109 | 2009-06-08 12:37:39 +0900 | [diff] [blame] | 237 | * tomoyo_name_list is used for holding string data used by TOMOYO. |
| 238 | * Since same string data is likely used for multiple times (e.g. |
| 239 | * "/lib/libc-2.5.so"), TOMOYO shares string data in the form of |
| 240 | * "const struct tomoyo_path_info *". |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 241 | */ |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 242 | struct list_head tomoyo_name_list[TOMOYO_MAX_HASH]; |
| 243 | /* Lock for protecting tomoyo_name_list . */ |
| 244 | DEFINE_MUTEX(tomoyo_name_list_lock); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 245 | |
| 246 | /** |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 247 | * tomoyo_get_name - Allocate permanent memory for string data. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 248 | * |
| 249 | * @name: The string to store into the permernent memory. |
| 250 | * |
| 251 | * Returns pointer to "struct tomoyo_path_info" on success, NULL otherwise. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 252 | */ |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 253 | const struct tomoyo_path_info *tomoyo_get_name(const char *name) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 254 | { |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 255 | struct tomoyo_name_entry *ptr; |
| 256 | unsigned int hash; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 257 | int len; |
Tetsuo Handa | e41035a | 2010-01-05 06:39:00 +0900 | [diff] [blame] | 258 | int allocated_len; |
Stephen Hemminger | 024e1a4 | 2009-10-27 19:24:46 -0700 | [diff] [blame] | 259 | struct list_head *head; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 260 | |
| 261 | if (!name) |
| 262 | return NULL; |
| 263 | len = strlen(name) + 1; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 264 | hash = full_name_hash((const unsigned char *) name, len - 1); |
Stephen Hemminger | 024e1a4 | 2009-10-27 19:24:46 -0700 | [diff] [blame] | 265 | head = &tomoyo_name_list[hash_long(hash, TOMOYO_HASH_BITS)]; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 266 | mutex_lock(&tomoyo_name_list_lock); |
Stephen Hemminger | 024e1a4 | 2009-10-27 19:24:46 -0700 | [diff] [blame] | 267 | list_for_each_entry(ptr, head, list) { |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 268 | if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name)) |
| 269 | continue; |
| 270 | atomic_inc(&ptr->users); |
| 271 | goto out; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 272 | } |
Tetsuo Handa | e41035a | 2010-01-05 06:39:00 +0900 | [diff] [blame] | 273 | ptr = kzalloc(sizeof(*ptr) + len, GFP_KERNEL); |
| 274 | allocated_len = ptr ? ksize(ptr) : 0; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 275 | if (!ptr || (tomoyo_quota_for_policy && |
| 276 | atomic_read(&tomoyo_policy_memory_size) + allocated_len |
| 277 | > tomoyo_quota_for_policy)) { |
Tetsuo Handa | e41035a | 2010-01-05 06:39:00 +0900 | [diff] [blame] | 278 | kfree(ptr); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 279 | printk(KERN_WARNING "ERROR: Out of memory " |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 280 | "for tomoyo_get_name().\n"); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 281 | if (!tomoyo_policy_loaded) |
| 282 | panic("MAC Initialization failed.\n"); |
| 283 | ptr = NULL; |
| 284 | goto out; |
| 285 | } |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 286 | atomic_add(allocated_len, &tomoyo_policy_memory_size); |
Tetsuo Handa | e41035a | 2010-01-05 06:39:00 +0900 | [diff] [blame] | 287 | ptr->entry.name = ((char *) ptr) + sizeof(*ptr); |
| 288 | memmove((char *) ptr->entry.name, name, len); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 289 | atomic_set(&ptr->users, 1); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 290 | tomoyo_fill_path_info(&ptr->entry); |
Stephen Hemminger | 024e1a4 | 2009-10-27 19:24:46 -0700 | [diff] [blame] | 291 | list_add_tail(&ptr->list, head); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 292 | out: |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 293 | mutex_unlock(&tomoyo_name_list_lock); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 294 | return ptr ? &ptr->entry : NULL; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * tomoyo_realpath_init - Initialize realpath related code. |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 299 | */ |
Tetsuo Handa | 1581e7d | 2009-02-21 20:40:50 +0900 | [diff] [blame] | 300 | void __init tomoyo_realpath_init(void) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 301 | { |
| 302 | int i; |
| 303 | |
| 304 | BUILD_BUG_ON(TOMOYO_MAX_PATHNAME_LEN > PATH_MAX); |
| 305 | for (i = 0; i < TOMOYO_MAX_HASH; i++) |
| 306 | INIT_LIST_HEAD(&tomoyo_name_list[i]); |
| 307 | INIT_LIST_HEAD(&tomoyo_kernel_domain.acl_info_list); |
Tetsuo Handa | bf24fb0 | 2010-02-11 09:41:58 +0900 | [diff] [blame] | 308 | tomoyo_kernel_domain.domainname = tomoyo_get_name(TOMOYO_ROOT_NAME); |
Tetsuo Handa | fdb8ebb | 2009-12-08 09:34:43 +0900 | [diff] [blame] | 309 | /* |
| 310 | * tomoyo_read_lock() is not needed because this function is |
| 311 | * called before the first "delete" request. |
| 312 | */ |
| 313 | list_add_tail_rcu(&tomoyo_kernel_domain.list, &tomoyo_domain_list); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 314 | if (tomoyo_find_domain(TOMOYO_ROOT_NAME) != &tomoyo_kernel_domain) |
| 315 | panic("Can't register tomoyo_kernel_domain"); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 316 | } |
| 317 | |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 318 | /** |
| 319 | * tomoyo_read_memory_counter - Check for memory usage in bytes. |
| 320 | * |
| 321 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 322 | * |
| 323 | * Returns memory usage. |
| 324 | */ |
| 325 | int tomoyo_read_memory_counter(struct tomoyo_io_buffer *head) |
| 326 | { |
| 327 | if (!head->read_eof) { |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 328 | const unsigned int policy |
| 329 | = atomic_read(&tomoyo_policy_memory_size); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 330 | char buffer[64]; |
| 331 | |
| 332 | memset(buffer, 0, sizeof(buffer)); |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 333 | if (tomoyo_quota_for_policy) |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 334 | snprintf(buffer, sizeof(buffer) - 1, |
| 335 | " (Quota: %10u)", |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 336 | tomoyo_quota_for_policy); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 337 | else |
| 338 | buffer[0] = '\0'; |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 339 | tomoyo_io_printf(head, "Policy: %10u%s\n", policy, buffer); |
| 340 | tomoyo_io_printf(head, "Total: %10u\n", policy); |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 341 | head->read_eof = true; |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * tomoyo_write_memory_quota - Set memory quota. |
| 348 | * |
| 349 | * @head: Pointer to "struct tomoyo_io_buffer". |
| 350 | * |
| 351 | * Returns 0. |
| 352 | */ |
| 353 | int tomoyo_write_memory_quota(struct tomoyo_io_buffer *head) |
| 354 | { |
| 355 | char *data = head->write_buf; |
| 356 | unsigned int size; |
| 357 | |
Tetsuo Handa | 847b173 | 2010-02-11 09:43:54 +0900 | [diff] [blame] | 358 | if (sscanf(data, "Policy: %u", &size) == 1) |
| 359 | tomoyo_quota_for_policy = size; |
Kentaro Takeda | c73bd6d | 2009-02-05 17:18:12 +0900 | [diff] [blame] | 360 | return 0; |
| 361 | } |