namei.c (8e96e3b7b8407be794ab1fd8e4b332818a358e78) namei.c (e419b4cc585680940bc42f8ca8a071d6023fb1bb)
1/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.

--- 214 unchanged lines hidden (view full) ---

223
224/*
225 * This does the basic permission checking
226 */
227static int acl_permission_check(struct inode *inode, int mask)
228{
229 unsigned int mode = inode->i_mode;
230
1/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.

--- 214 unchanged lines hidden (view full) ---

223
224/*
225 * This does the basic permission checking
226 */
227static int acl_permission_check(struct inode *inode, int mask)
228{
229 unsigned int mode = inode->i_mode;
230
231 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
231 if (current_user_ns() != inode_userns(inode))
232 goto other_perms;
233
234 if (likely(current_fsuid() == inode->i_uid))
232 mode >>= 6;
233 else {
234 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
235 int error = check_acl(inode, mask);
236 if (error != -EAGAIN)
237 return error;
238 }
239
240 if (in_group_p(inode->i_gid))
241 mode >>= 3;
242 }
243
235 mode >>= 6;
236 else {
237 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
238 int error = check_acl(inode, mask);
239 if (error != -EAGAIN)
240 return error;
241 }
242
243 if (in_group_p(inode->i_gid))
244 mode >>= 3;
245 }
246
247other_perms:
244 /*
245 * If the DACs are ok we don't need any capability check.
246 */
247 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
248 return 0;
249 return -EACCES;
250}
251

--- 19 unchanged lines hidden (view full) ---

271 * Do the basic permission checks.
272 */
273 ret = acl_permission_check(inode, mask);
274 if (ret != -EACCES)
275 return ret;
276
277 if (S_ISDIR(inode->i_mode)) {
278 /* DACs are overridable for directories */
248 /*
249 * If the DACs are ok we don't need any capability check.
250 */
251 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
252 return 0;
253 return -EACCES;
254}
255

--- 19 unchanged lines hidden (view full) ---

275 * Do the basic permission checks.
276 */
277 ret = acl_permission_check(inode, mask);
278 if (ret != -EACCES)
279 return ret;
280
281 if (S_ISDIR(inode->i_mode)) {
282 /* DACs are overridable for directories */
279 if (inode_capable(inode, CAP_DAC_OVERRIDE))
283 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
280 return 0;
281 if (!(mask & MAY_WRITE))
284 return 0;
285 if (!(mask & MAY_WRITE))
282 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
286 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
283 return 0;
284 return -EACCES;
285 }
286 /*
287 * Read/write DACs are always overridable.
288 * Executable DACs are overridable when there is
289 * at least one exec bit set.
290 */
291 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
287 return 0;
288 return -EACCES;
289 }
290 /*
291 * Read/write DACs are always overridable.
292 * Executable DACs are overridable when there is
293 * at least one exec bit set.
294 */
295 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
292 if (inode_capable(inode, CAP_DAC_OVERRIDE))
296 if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE))
293 return 0;
294
295 /*
296 * Searching includes executable on directories, else just read.
297 */
298 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
299 if (mask == MAY_READ)
297 return 0;
298
299 /*
300 * Searching includes executable on directories, else just read.
301 */
302 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
303 if (mask == MAY_READ)
300 if (inode_capable(inode, CAP_DAC_READ_SEARCH))
304 if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH))
301 return 0;
302
303 return -EACCES;
304}
305
306/*
307 * We _really_ want to just do "generic_permission()" without
308 * even looking at the inode->i_op values. So we keep a cache

--- 1089 unchanged lines hidden (view full) ---

1398 *
1399 * - Furthermore, we need an efficient 64-bit compile for the
1400 * 64-bit case in order to generate the "number of bytes in
1401 * the final mask". Again, that could be replaced with a
1402 * efficient population count instruction or similar.
1403 */
1404#ifdef CONFIG_DCACHE_WORD_ACCESS
1405
305 return 0;
306
307 return -EACCES;
308}
309
310/*
311 * We _really_ want to just do "generic_permission()" without
312 * even looking at the inode->i_op values. So we keep a cache

--- 1089 unchanged lines hidden (view full) ---

1402 *
1403 * - Furthermore, we need an efficient 64-bit compile for the
1404 * 64-bit case in order to generate the "number of bytes in
1405 * the final mask". Again, that could be replaced with a
1406 * efficient population count instruction or similar.
1407 */
1408#ifdef CONFIG_DCACHE_WORD_ACCESS
1409
1410#include <asm/word-at-a-time.h>
1411
1406#ifdef CONFIG_64BIT
1407
1412#ifdef CONFIG_64BIT
1413
1408/*
1409 * Jan Achrenius on G+: microoptimized version of
1410 * the simpler "(mask & ONEBYTES) * ONEBYTES >> 56"
1411 * that works for the bytemasks without having to
1412 * mask them first.
1413 */
1414static inline long count_masked_bytes(unsigned long mask)
1415{
1416 return mask*0x0001020304050608ul >> 56;
1417}
1418
1419static inline unsigned int fold_hash(unsigned long hash)
1420{
1421 hash += hash >> (8*sizeof(int));
1422 return hash;
1423}
1424
1425#else /* 32-bit case */
1426
1414static inline unsigned int fold_hash(unsigned long hash)
1415{
1416 hash += hash >> (8*sizeof(int));
1417 return hash;
1418}
1419
1420#else /* 32-bit case */
1421
1427/* Carl Chatfield / Jan Achrenius G+ version for 32-bit */
1428static inline long count_masked_bytes(long mask)
1429{
1430 /* (000000 0000ff 00ffff ffffff) -> ( 1 1 2 3 ) */
1431 long a = (0x0ff0001+mask) >> 23;
1432 /* Fix the 1 for 00 case */
1433 return a & mask;
1434}
1435
1436#define fold_hash(x) (x)
1437
1438#endif
1439
1440unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1441{
1442 unsigned long a, mask;
1443 unsigned long hash = 0;
1444
1445 for (;;) {
1422#define fold_hash(x) (x)
1423
1424#endif
1425
1426unsigned int full_name_hash(const unsigned char *name, unsigned int len)
1427{
1428 unsigned long a, mask;
1429 unsigned long hash = 0;
1430
1431 for (;;) {
1446 a = *(unsigned long *)name;
1432 a = load_unaligned_zeropad(name);
1447 if (len < sizeof(unsigned long))
1448 break;
1449 hash += a;
1450 hash *= 9;
1451 name += sizeof(unsigned long);
1452 len -= sizeof(unsigned long);
1453 if (!len)
1454 goto done;
1455 }
1456 mask = ~(~0ul << len*8);
1457 hash += mask & a;
1458done:
1459 return fold_hash(hash);
1460}
1461EXPORT_SYMBOL(full_name_hash);
1462
1433 if (len < sizeof(unsigned long))
1434 break;
1435 hash += a;
1436 hash *= 9;
1437 name += sizeof(unsigned long);
1438 len -= sizeof(unsigned long);
1439 if (!len)
1440 goto done;
1441 }
1442 mask = ~(~0ul << len*8);
1443 hash += mask & a;
1444done:
1445 return fold_hash(hash);
1446}
1447EXPORT_SYMBOL(full_name_hash);
1448
1463#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
1464#define ONEBYTES REPEAT_BYTE(0x01)
1465#define SLASHBYTES REPEAT_BYTE('/')
1466#define HIGHBITS REPEAT_BYTE(0x80)
1467
1468/* Return the high bit set in the first byte that is a zero */
1469static inline unsigned long has_zero(unsigned long a)
1470{
1471 return ((a - ONEBYTES) & ~a) & HIGHBITS;
1472}
1473
1474/*
1475 * Calculate the length and hash of the path component, and
1476 * return the length of the component;
1477 */
1478static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1479{
1480 unsigned long a, mask, hash, len;
1481
1482 hash = a = 0;
1483 len = -sizeof(unsigned long);
1484 do {
1485 hash = (hash + a) * 9;
1486 len += sizeof(unsigned long);
1449/*
1450 * Calculate the length and hash of the path component, and
1451 * return the length of the component;
1452 */
1453static inline unsigned long hash_name(const char *name, unsigned int *hashp)
1454{
1455 unsigned long a, mask, hash, len;
1456
1457 hash = a = 0;
1458 len = -sizeof(unsigned long);
1459 do {
1460 hash = (hash + a) * 9;
1461 len += sizeof(unsigned long);
1487 a = *(unsigned long *)(name+len);
1462 a = load_unaligned_zeropad(name+len);
1488 /* Do we have any NUL or '/' bytes in this word? */
1463 /* Do we have any NUL or '/' bytes in this word? */
1489 mask = has_zero(a) | has_zero(a ^ SLASHBYTES);
1464 mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/'));
1490 } while (!mask);
1491
1492 /* The mask *below* the first high bit set */
1493 mask = (mask - 1) & ~mask;
1494 mask >>= 7;
1495 hash += a & mask;
1496 *hashp = fold_hash(hash);
1497

--- 453 unchanged lines hidden (view full) ---

1951}
1952
1953/*
1954 * It's inline, so penalty for filesystems that don't use sticky bit is
1955 * minimal.
1956 */
1957static inline int check_sticky(struct inode *dir, struct inode *inode)
1958{
1465 } while (!mask);
1466
1467 /* The mask *below* the first high bit set */
1468 mask = (mask - 1) & ~mask;
1469 mask >>= 7;
1470 hash += a & mask;
1471 *hashp = fold_hash(hash);
1472

--- 453 unchanged lines hidden (view full) ---

1926}
1927
1928/*
1929 * It's inline, so penalty for filesystems that don't use sticky bit is
1930 * minimal.
1931 */
1932static inline int check_sticky(struct inode *dir, struct inode *inode)
1933{
1959 kuid_t fsuid = current_fsuid();
1934 uid_t fsuid = current_fsuid();
1960
1961 if (!(dir->i_mode & S_ISVTX))
1962 return 0;
1935
1936 if (!(dir->i_mode & S_ISVTX))
1937 return 0;
1963 if (uid_eq(inode->i_uid, fsuid))
1938 if (current_user_ns() != inode_userns(inode))
1939 goto other_userns;
1940 if (inode->i_uid == fsuid)
1964 return 0;
1941 return 0;
1965 if (uid_eq(dir->i_uid, fsuid))
1942 if (dir->i_uid == fsuid)
1966 return 0;
1943 return 0;
1967 return !inode_capable(inode, CAP_FOWNER);
1944
1945other_userns:
1946 return !ns_capable(inode_userns(inode), CAP_FOWNER);
1968}
1969
1970/*
1971 * Check whether we can remove a link victim from directory dir, check
1972 * whether the type of victim is right.
1973 * 1. We can't do it if dir is read-only (done in permission())
1974 * 2. We should have write and exec permissions on dir
1975 * 3. We can't remove anything from append-only dir

--- 571 unchanged lines hidden (view full) ---

2547
2548int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2549{
2550 int error = may_create(dir, dentry);
2551
2552 if (error)
2553 return error;
2554
1947}
1948
1949/*
1950 * Check whether we can remove a link victim from directory dir, check
1951 * whether the type of victim is right.
1952 * 1. We can't do it if dir is read-only (done in permission())
1953 * 2. We should have write and exec permissions on dir
1954 * 3. We can't remove anything from append-only dir

--- 571 unchanged lines hidden (view full) ---

2526
2527int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
2528{
2529 int error = may_create(dir, dentry);
2530
2531 if (error)
2532 return error;
2533
2555 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2534 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
2535 !ns_capable(inode_userns(dir), CAP_MKNOD))
2556 return -EPERM;
2557
2558 if (!dir->i_op->mknod)
2559 return -EPERM;
2560
2561 error = devcgroup_inode_mknod(mode, dev);
2562 if (error)
2563 return error;

--- 958 unchanged lines hidden ---
2536 return -EPERM;
2537
2538 if (!dir->i_op->mknod)
2539 return -EPERM;
2540
2541 error = devcgroup_inode_mknod(mode, dev);
2542 if (error)
2543 return error;

--- 958 unchanged lines hidden ---