10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*12273SCasper.Dik@Sun.COM * Common Development and Distribution License (the "License").
6*12273SCasper.Dik@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12273SCasper.Dik@Sun.COM * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/errno.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/t_lock.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/debug.h>
320Sstevel@tonic-gate #include <sys/time.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/vnode.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/kmem.h>
390Sstevel@tonic-gate #include <sys/atomic.h>
400Sstevel@tonic-gate #include <sys/policy.h>
410Sstevel@tonic-gate #include <sys/fs/tmp.h>
420Sstevel@tonic-gate #include <sys/fs/tmpnode.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define MODESHIFT 3
450Sstevel@tonic-gate
460Sstevel@tonic-gate int
tmp_taccess(void * vtp,int mode,struct cred * cred)470Sstevel@tonic-gate tmp_taccess(void *vtp, int mode, struct cred *cred)
480Sstevel@tonic-gate {
490Sstevel@tonic-gate struct tmpnode *tp = vtp;
500Sstevel@tonic-gate int shift = 0;
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * Check access based on owner, group and
530Sstevel@tonic-gate * public permissions in tmpnode.
540Sstevel@tonic-gate */
550Sstevel@tonic-gate if (crgetuid(cred) != tp->tn_uid) {
560Sstevel@tonic-gate shift += MODESHIFT;
570Sstevel@tonic-gate if (groupmember(tp->tn_gid, cred) == 0)
580Sstevel@tonic-gate shift += MODESHIFT;
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
61*12273SCasper.Dik@Sun.COM return (secpolicy_vnode_access2(cred, TNTOV(tp), tp->tn_uid,
62*12273SCasper.Dik@Sun.COM tp->tn_mode << shift, mode));
630Sstevel@tonic-gate }
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * Decide whether it is okay to remove within a sticky directory.
670Sstevel@tonic-gate * Two conditions need to be met: write access to the directory
680Sstevel@tonic-gate * is needed. In sticky directories, write access is not sufficient;
690Sstevel@tonic-gate * you can remove entries from a directory only if you own the directory,
700Sstevel@tonic-gate * if you are privileged, if you own the entry or if they entry is
710Sstevel@tonic-gate * a plain file and you have write access to that file.
720Sstevel@tonic-gate * Function returns 0 if remove access is granted.
730Sstevel@tonic-gate */
740Sstevel@tonic-gate
750Sstevel@tonic-gate int
tmp_sticky_remove_access(struct tmpnode * dir,struct tmpnode * entry,struct cred * cr)760Sstevel@tonic-gate tmp_sticky_remove_access(struct tmpnode *dir, struct tmpnode *entry,
770Sstevel@tonic-gate struct cred *cr)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate uid_t uid = crgetuid(cr);
800Sstevel@tonic-gate
810Sstevel@tonic-gate if ((dir->tn_mode & S_ISVTX) &&
820Sstevel@tonic-gate uid != dir->tn_uid &&
830Sstevel@tonic-gate uid != entry->tn_uid &&
840Sstevel@tonic-gate (entry->tn_type != VREG ||
850Sstevel@tonic-gate tmp_taccess(entry, VWRITE, cr) != 0))
860Sstevel@tonic-gate return (secpolicy_vnode_remove(cr));
870Sstevel@tonic-gate
880Sstevel@tonic-gate return (0);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Allocate zeroed memory if tmpfs_maxkmem has not been exceeded
930Sstevel@tonic-gate * or the 'musthave' flag is set. 'musthave' allocations should
940Sstevel@tonic-gate * always be subordinate to normal allocations so that tmpfs_maxkmem
950Sstevel@tonic-gate * can't be exceeded by more than a few KB. Example: when creating
960Sstevel@tonic-gate * a new directory, the tmpnode is a normal allocation; if that
970Sstevel@tonic-gate * succeeds, the dirents for "." and ".." are 'musthave' allocations.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate void *
tmp_memalloc(size_t size,int musthave)1000Sstevel@tonic-gate tmp_memalloc(size_t size, int musthave)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate static time_t last_warning;
1030Sstevel@tonic-gate time_t now;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate if (atomic_add_long_nv(&tmp_kmemspace, size) < tmpfs_maxkmem ||
1060Sstevel@tonic-gate musthave)
1070Sstevel@tonic-gate return (kmem_zalloc(size, KM_SLEEP));
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate atomic_add_long(&tmp_kmemspace, -size);
1100Sstevel@tonic-gate now = gethrestime_sec();
1110Sstevel@tonic-gate if (last_warning != now) {
1120Sstevel@tonic-gate last_warning = now;
1130Sstevel@tonic-gate cmn_err(CE_WARN, "tmp_memalloc: tmpfs over memory limit");
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate return (NULL);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate void
tmp_memfree(void * cp,size_t size)1190Sstevel@tonic-gate tmp_memfree(void *cp, size_t size)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate kmem_free(cp, size);
1220Sstevel@tonic-gate atomic_add_long(&tmp_kmemspace, -size);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /*
1260Sstevel@tonic-gate * Convert a string containing a number (number of bytes) to a pgcnt_t,
1270Sstevel@tonic-gate * containing the corresponding number of pages. On 32-bit kernels, the
1280Sstevel@tonic-gate * maximum value encoded in 'str' is PAGESIZE * ULONG_MAX, while the value
1290Sstevel@tonic-gate * returned in 'maxpg' is at most ULONG_MAX.
1300Sstevel@tonic-gate *
1310Sstevel@tonic-gate * If the number is followed by a "k" or "K", the value is converted from
1320Sstevel@tonic-gate * kilobytes to bytes. If it is followed by an "m" or "M" it is converted
1330Sstevel@tonic-gate * from megabytes to bytes. If it is not followed by a character it is
1340Sstevel@tonic-gate * assumed to be in bytes. Multiple letter options are allowed, so for instance
1350Sstevel@tonic-gate * '2mk' is interpreted as 2gb.
1360Sstevel@tonic-gate *
1370Sstevel@tonic-gate * Parse and overflow errors are detected and a non-zero number returned on
1380Sstevel@tonic-gate * error.
1390Sstevel@tonic-gate */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate int
tmp_convnum(char * str,pgcnt_t * maxpg)1420Sstevel@tonic-gate tmp_convnum(char *str, pgcnt_t *maxpg)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate uint64_t num = 0, oldnum;
1450Sstevel@tonic-gate #ifdef _LP64
1460Sstevel@tonic-gate uint64_t max_bytes = ULONG_MAX;
1470Sstevel@tonic-gate #else
1480Sstevel@tonic-gate uint64_t max_bytes = PAGESIZE * (uint64_t)ULONG_MAX;
1490Sstevel@tonic-gate #endif
1500Sstevel@tonic-gate char *c;
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate if (str == NULL)
1530Sstevel@tonic-gate return (EINVAL);
1540Sstevel@tonic-gate c = str;
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * Convert str to number
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate while ((*c >= '0') && (*c <= '9')) {
1600Sstevel@tonic-gate oldnum = num;
1610Sstevel@tonic-gate num = num * 10 + (*c++ - '0');
1620Sstevel@tonic-gate if (oldnum > num) /* overflow */
1630Sstevel@tonic-gate return (EINVAL);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate * Terminate on null
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate while (*c != '\0') {
1700Sstevel@tonic-gate switch (*c++) {
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * convert from kilobytes
1740Sstevel@tonic-gate */
1750Sstevel@tonic-gate case 'k':
1760Sstevel@tonic-gate case 'K':
1770Sstevel@tonic-gate if (num > max_bytes / 1024) /* will overflow */
1780Sstevel@tonic-gate return (EINVAL);
1790Sstevel@tonic-gate num *= 1024;
1800Sstevel@tonic-gate break;
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * convert from megabytes
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate case 'm':
1860Sstevel@tonic-gate case 'M':
1870Sstevel@tonic-gate if (num > max_bytes / (1024 * 1024)) /* will overflow */
1880Sstevel@tonic-gate return (EINVAL);
1890Sstevel@tonic-gate num *= 1024 * 1024;
1900Sstevel@tonic-gate break;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate default:
1930Sstevel@tonic-gate return (EINVAL);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate * Since btopr() rounds up to page granularity, this round-up can
1990Sstevel@tonic-gate * cause an overflow only if 'num' is between (max_bytes - PAGESIZE)
2000Sstevel@tonic-gate * and (max_bytes). In this case the resulting number is zero, which
2010Sstevel@tonic-gate * is what we check for below.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if ((*maxpg = (pgcnt_t)btopr(num)) == 0 && num != 0)
2040Sstevel@tonic-gate return (EINVAL);
2050Sstevel@tonic-gate return (0);
2060Sstevel@tonic-gate }
207