acl.c 9.6 KB
Newer Older
1 2
/*
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright © 2006  NEC Corporation
5
 *
6 7 8 9 10
 * Created by KaiGai Kohei <kaigai@ak.jp.nec.com>
 *
 * For licensing information, see the file 'LICENCE' in this directory.
 *
 */
11

12 13 14
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
15
#include <linux/sched.h>
16 17 18 19 20 21 22 23 24 25 26
#include <linux/time.h>
#include <linux/crc32.h>
#include <linux/jffs2.h>
#include <linux/xattr.h>
#include <linux/posix_acl_xattr.h>
#include <linux/mtd/mtd.h>
#include "nodelist.h"

static size_t jffs2_acl_size(int count)
{
	if (count <= 4) {
27 28
		return sizeof(struct jffs2_acl_header)
		       + count * sizeof(struct jffs2_acl_entry_short);
29
	} else {
30 31 32
		return sizeof(struct jffs2_acl_header)
		       + 4 * sizeof(struct jffs2_acl_entry_short)
		       + (count - 4) * sizeof(struct jffs2_acl_entry);
33 34 35 36 37 38 39
	}
}

static int jffs2_acl_count(size_t size)
{
	size_t s;

40
	size -= sizeof(struct jffs2_acl_header);
41
	if (size < 4 * sizeof(struct jffs2_acl_entry_short)) {
42
		if (size % sizeof(struct jffs2_acl_entry_short))
43
			return -1;
44
		return size / sizeof(struct jffs2_acl_entry_short);
45
	} else {
46
		s = size - 4 * sizeof(struct jffs2_acl_entry_short);
47
		if (s % sizeof(struct jffs2_acl_entry))
48
			return -1;
49
		return s / sizeof(struct jffs2_acl_entry) + 4;
50 51 52
	}
}

53
static struct posix_acl *jffs2_acl_from_medium(void *value, size_t size)
54
{
55 56 57
	void *end = value + size;
	struct jffs2_acl_header *header = value;
	struct jffs2_acl_entry *entry;
58 59 60 61 62 63
	struct posix_acl *acl;
	uint32_t ver;
	int i, count;

	if (!value)
		return NULL;
64
	if (size < sizeof(struct jffs2_acl_header))
65
		return ERR_PTR(-EINVAL);
66
	ver = je32_to_cpu(header->a_version);
67 68 69 70 71
	if (ver != JFFS2_ACL_VERSION) {
		JFFS2_WARNING("Invalid ACL version. (=%u)\n", ver);
		return ERR_PTR(-EINVAL);
	}

72
	value += sizeof(struct jffs2_acl_header);
73 74 75 76 77 78 79 80 81 82 83
	count = jffs2_acl_count(size);
	if (count < 0)
		return ERR_PTR(-EINVAL);
	if (count == 0)
		return NULL;

	acl = posix_acl_alloc(count, GFP_KERNEL);
	if (!acl)
		return ERR_PTR(-ENOMEM);

	for (i=0; i < count; i++) {
84 85
		entry = value;
		if (value + sizeof(struct jffs2_acl_entry_short) > end)
86 87 88 89 90 91 92 93
			goto fail;
		acl->a_entries[i].e_tag = je16_to_cpu(entry->e_tag);
		acl->a_entries[i].e_perm = je16_to_cpu(entry->e_perm);
		switch (acl->a_entries[i].e_tag) {
			case ACL_USER_OBJ:
			case ACL_GROUP_OBJ:
			case ACL_MASK:
			case ACL_OTHER:
94
				value += sizeof(struct jffs2_acl_entry_short);
95 96 97 98 99
				acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
				break;

			case ACL_USER:
			case ACL_GROUP:
100 101
				value += sizeof(struct jffs2_acl_entry);
				if (value > end)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
					goto fail;
				acl->a_entries[i].e_id = je32_to_cpu(entry->e_id);
				break;

			default:
				goto fail;
		}
	}
	if (value != end)
		goto fail;
	return acl;
 fail:
	posix_acl_release(acl);
	return ERR_PTR(-EINVAL);
}

static void *jffs2_acl_to_medium(const struct posix_acl *acl, size_t *size)
{
120 121 122
	struct jffs2_acl_header *header;
	struct jffs2_acl_entry *entry;
	void *e;
123 124 125
	size_t i;

	*size = jffs2_acl_size(acl->a_count);
126 127
	header = kmalloc(sizeof(*header) + acl->a_count * sizeof(*entry), GFP_KERNEL);
	if (!header)
128
		return ERR_PTR(-ENOMEM);
129 130
	header->a_version = cpu_to_je32(JFFS2_ACL_VERSION);
	e = header + 1;
131
	for (i=0; i < acl->a_count; i++) {
132
		entry = e;
133 134 135 136 137 138
		entry->e_tag = cpu_to_je16(acl->a_entries[i].e_tag);
		entry->e_perm = cpu_to_je16(acl->a_entries[i].e_perm);
		switch(acl->a_entries[i].e_tag) {
			case ACL_USER:
			case ACL_GROUP:
				entry->e_id = cpu_to_je32(acl->a_entries[i].e_id);
139
				e += sizeof(struct jffs2_acl_entry);
140 141 142 143 144 145
				break;

			case ACL_USER_OBJ:
			case ACL_GROUP_OBJ:
			case ACL_MASK:
			case ACL_OTHER:
146
				e += sizeof(struct jffs2_acl_entry_short);
147 148 149 150 151 152
				break;

			default:
				goto fail;
		}
	}
153
	return header;
154
 fail:
155
	kfree(header);
156 157 158
	return ERR_PTR(-EINVAL);
}

A
Adrian Bunk 已提交
159
static struct posix_acl *jffs2_get_acl(struct inode *inode, int type)
160 161 162 163 164
{
	struct posix_acl *acl;
	char *value = NULL;
	int rc, xprefix;

165 166 167 168
	acl = get_cached_acl(inode, type);
	if (acl != ACL_NOT_CACHED)
		return acl;

169 170 171 172 173 174 175 176
	switch (type) {
	case ACL_TYPE_ACCESS:
		xprefix = JFFS2_XPREFIX_ACL_ACCESS;
		break;
	case ACL_TYPE_DEFAULT:
		xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
		break;
	default:
177
		BUG();
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	}
	rc = do_jffs2_getxattr(inode, xprefix, "", NULL, 0);
	if (rc > 0) {
		value = kmalloc(rc, GFP_KERNEL);
		if (!value)
			return ERR_PTR(-ENOMEM);
		rc = do_jffs2_getxattr(inode, xprefix, "", value, rc);
	}
	if (rc > 0) {
		acl = jffs2_acl_from_medium(value, rc);
	} else if (rc == -ENODATA || rc == -ENOSYS) {
		acl = NULL;
	} else {
		acl = ERR_PTR(rc);
	}
	if (value)
		kfree(value);
195 196
	if (!IS_ERR(acl))
		set_cached_acl(inode, type, acl);
197 198 199
	return acl;
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static int __jffs2_set_acl(struct inode *inode, int xprefix, struct posix_acl *acl)
{
	char *value = NULL;
	size_t size = 0;
	int rc;

	if (acl) {
		value = jffs2_acl_to_medium(acl, &size);
		if (IS_ERR(value))
			return PTR_ERR(value);
	}
	rc = do_jffs2_setxattr(inode, xprefix, "", value, size, 0);
	if (!value && rc == -ENODATA)
		rc = 0;
	kfree(value);

	return rc;
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
static int jffs2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
{
	int rc, xprefix;

	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;

	switch (type) {
	case ACL_TYPE_ACCESS:
		xprefix = JFFS2_XPREFIX_ACL_ACCESS;
		if (acl) {
			mode_t mode = inode->i_mode;
			rc = posix_acl_equiv_mode(acl, &mode);
			if (rc < 0)
				return rc;
			if (inode->i_mode != mode) {
235 236
				struct iattr attr;

237
				attr.ia_valid = ATTR_MODE | ATTR_CTIME;
238
				attr.ia_mode = mode;
239
				attr.ia_ctime = CURRENT_TIME_SEC;
240 241 242
				rc = jffs2_do_setattr(inode, &attr);
				if (rc < 0)
					return rc;
243 244 245 246 247 248 249 250 251 252 253 254 255
			}
			if (rc == 0)
				acl = NULL;
		}
		break;
	case ACL_TYPE_DEFAULT:
		xprefix = JFFS2_XPREFIX_ACL_DEFAULT;
		if (!S_ISDIR(inode->i_mode))
			return acl ? -EACCES : 0;
		break;
	default:
		return -EINVAL;
	}
256
	rc = __jffs2_set_acl(inode, xprefix, acl);
257 258
	if (!rc)
		set_cached_acl(inode, type, acl);
259 260 261
	return rc;
}

262
int jffs2_check_acl(struct inode *inode, int mask, unsigned int flags)
263 264 265 266
{
	struct posix_acl *acl;
	int rc;

267 268 269
	if (flags & IPERM_FLAG_RCU)
		return -ECHILD;

270 271 272 273 274 275 276 277 278 279 280
	acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
	if (IS_ERR(acl))
		return PTR_ERR(acl);
	if (acl) {
		rc = posix_acl_permission(inode, acl, mask);
		posix_acl_release(acl);
		return rc;
	}
	return -EAGAIN;
}

281
int jffs2_init_acl_pre(struct inode *dir_i, struct inode *inode, int *i_mode)
282
{
283 284
	struct posix_acl *acl, *clone;
	int rc;
285

286
	cache_no_acl(inode);
287 288 289 290 291 292 293 294 295

	if (S_ISLNK(*i_mode))
		return 0;	/* Symlink always has no-ACL */

	acl = jffs2_get_acl(dir_i, ACL_TYPE_DEFAULT);
	if (IS_ERR(acl))
		return PTR_ERR(acl);

	if (!acl) {
A
Al Viro 已提交
296
		*i_mode &= ~current_umask();
297 298
	} else {
		if (S_ISDIR(*i_mode))
299
			set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
300

301 302
		clone = posix_acl_clone(acl, GFP_KERNEL);
		if (!clone)
303 304
			return -ENOMEM;
		rc = posix_acl_create_masq(clone, (mode_t *)i_mode);
305 306
		if (rc < 0) {
			posix_acl_release(clone);
307
			return rc;
308
		}
309
		if (rc > 0)
310
			set_cached_acl(inode, ACL_TYPE_ACCESS, clone);
311

312 313
		posix_acl_release(clone);
	}
314 315 316 317 318 319 320
	return 0;
}

int jffs2_init_acl_post(struct inode *inode)
{
	int rc;

A
Al Viro 已提交
321 322
	if (inode->i_default_acl) {
		rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_DEFAULT, inode->i_default_acl);
323 324 325 326
		if (rc)
			return rc;
	}

A
Al Viro 已提交
327 328
	if (inode->i_acl) {
		rc = __jffs2_set_acl(inode, JFFS2_XPREFIX_ACL_ACCESS, inode->i_acl);
329 330 331 332
		if (rc)
			return rc;
	}

333
	return 0;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
}

int jffs2_acl_chmod(struct inode *inode)
{
	struct posix_acl *acl, *clone;
	int rc;

	if (S_ISLNK(inode->i_mode))
		return -EOPNOTSUPP;
	acl = jffs2_get_acl(inode, ACL_TYPE_ACCESS);
	if (IS_ERR(acl) || !acl)
		return PTR_ERR(acl);
	clone = posix_acl_clone(acl, GFP_KERNEL);
	posix_acl_release(acl);
	if (!clone)
		return -ENOMEM;
	rc = posix_acl_chmod_masq(clone, inode->i_mode);
	if (!rc)
		rc = jffs2_set_acl(inode, ACL_TYPE_ACCESS, clone);
	posix_acl_release(clone);
	return rc;
}

357 358
static size_t jffs2_acl_access_listxattr(struct dentry *dentry, char *list,
		size_t list_size, const char *name, size_t name_len, int type)
359 360 361 362 363 364 365 366
{
	const int retlen = sizeof(POSIX_ACL_XATTR_ACCESS);

	if (list && retlen <= list_size)
		strcpy(list, POSIX_ACL_XATTR_ACCESS);
	return retlen;
}

367 368
static size_t jffs2_acl_default_listxattr(struct dentry *dentry, char *list,
		size_t list_size, const char *name, size_t name_len, int type)
369 370 371 372 373 374 375 376
{
	const int retlen = sizeof(POSIX_ACL_XATTR_DEFAULT);

	if (list && retlen <= list_size)
		strcpy(list, POSIX_ACL_XATTR_DEFAULT);
	return retlen;
}

377 378
static int jffs2_acl_getxattr(struct dentry *dentry, const char *name,
		void *buffer, size_t size, int type)
379 380 381 382
{
	struct posix_acl *acl;
	int rc;

383 384 385 386
	if (name[0] != '\0')
		return -EINVAL;

	acl = jffs2_get_acl(dentry->d_inode, type);
387 388 389 390 391 392 393 394 395 396
	if (IS_ERR(acl))
		return PTR_ERR(acl);
	if (!acl)
		return -ENODATA;
	rc = posix_acl_to_xattr(acl, buffer, size);
	posix_acl_release(acl);

	return rc;
}

397 398
static int jffs2_acl_setxattr(struct dentry *dentry, const char *name,
		const void *value, size_t size, int flags, int type)
399 400 401 402
{
	struct posix_acl *acl;
	int rc;

403 404 405
	if (name[0] != '\0')
		return -EINVAL;
	if (!is_owner_or_cap(dentry->d_inode))
406 407 408 409 410 411 412 413 414 415 416 417 418 419
		return -EPERM;

	if (value) {
		acl = posix_acl_from_xattr(value, size);
		if (IS_ERR(acl))
			return PTR_ERR(acl);
		if (acl) {
			rc = posix_acl_valid(acl);
			if (rc)
				goto out;
		}
	} else {
		acl = NULL;
	}
420
	rc = jffs2_set_acl(dentry->d_inode, type, acl);
421 422 423 424 425
 out:
	posix_acl_release(acl);
	return rc;
}

426
const struct xattr_handler jffs2_acl_access_xattr_handler = {
427
	.prefix	= POSIX_ACL_XATTR_ACCESS,
428
	.flags	= ACL_TYPE_DEFAULT,
429
	.list	= jffs2_acl_access_listxattr,
430 431
	.get	= jffs2_acl_getxattr,
	.set	= jffs2_acl_setxattr,
432 433
};

434
const struct xattr_handler jffs2_acl_default_xattr_handler = {
435
	.prefix	= POSIX_ACL_XATTR_DEFAULT,
436
	.flags	= ACL_TYPE_DEFAULT,
437
	.list	= jffs2_acl_default_listxattr,
438 439
	.get	= jffs2_acl_getxattr,
	.set	= jffs2_acl_setxattr,
440
};