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