17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*134a1f4eSCasper H.S. Dik * Common Development and Distribution License (the "License"). 6*134a1f4eSCasper H.S. Dik * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*134a1f4eSCasper H.S. Dik * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate #include <sys/types.h> 267c478bd9Sstevel@tonic-gate #include <sys/errno.h> 277c478bd9Sstevel@tonic-gate #include <sys/param.h> 287c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 297c478bd9Sstevel@tonic-gate #include <sys/systm.h> 307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 317c478bd9Sstevel@tonic-gate #include <sys/debug.h> 327c478bd9Sstevel@tonic-gate #include <sys/time.h> 337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 347c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 357c478bd9Sstevel@tonic-gate #include <sys/stat.h> 367c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 377c478bd9Sstevel@tonic-gate #include <sys/cred.h> 387c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 397c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 407c478bd9Sstevel@tonic-gate #include <sys/policy.h> 417c478bd9Sstevel@tonic-gate #include <sys/fs/tmp.h> 427c478bd9Sstevel@tonic-gate #include <sys/fs/tmpnode.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #define MODESHIFT 3 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate int 477c478bd9Sstevel@tonic-gate tmp_taccess(void *vtp, int mode, struct cred *cred) 487c478bd9Sstevel@tonic-gate { 497c478bd9Sstevel@tonic-gate struct tmpnode *tp = vtp; 507c478bd9Sstevel@tonic-gate int shift = 0; 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * Check access based on owner, group and 537c478bd9Sstevel@tonic-gate * public permissions in tmpnode. 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate if (crgetuid(cred) != tp->tn_uid) { 567c478bd9Sstevel@tonic-gate shift += MODESHIFT; 577c478bd9Sstevel@tonic-gate if (groupmember(tp->tn_gid, cred) == 0) 587c478bd9Sstevel@tonic-gate shift += MODESHIFT; 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 61*134a1f4eSCasper H.S. Dik return (secpolicy_vnode_access2(cred, TNTOV(tp), tp->tn_uid, 62*134a1f4eSCasper H.S. Dik tp->tn_mode << shift, mode)); 637c478bd9Sstevel@tonic-gate } 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Decide whether it is okay to remove within a sticky directory. 677c478bd9Sstevel@tonic-gate * Two conditions need to be met: write access to the directory 687c478bd9Sstevel@tonic-gate * is needed. In sticky directories, write access is not sufficient; 697c478bd9Sstevel@tonic-gate * you can remove entries from a directory only if you own the directory, 707c478bd9Sstevel@tonic-gate * if you are privileged, if you own the entry or if they entry is 717c478bd9Sstevel@tonic-gate * a plain file and you have write access to that file. 727c478bd9Sstevel@tonic-gate * Function returns 0 if remove access is granted. 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate int 767c478bd9Sstevel@tonic-gate tmp_sticky_remove_access(struct tmpnode *dir, struct tmpnode *entry, 777c478bd9Sstevel@tonic-gate struct cred *cr) 787c478bd9Sstevel@tonic-gate { 797c478bd9Sstevel@tonic-gate uid_t uid = crgetuid(cr); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if ((dir->tn_mode & S_ISVTX) && 827c478bd9Sstevel@tonic-gate uid != dir->tn_uid && 837c478bd9Sstevel@tonic-gate uid != entry->tn_uid && 847c478bd9Sstevel@tonic-gate (entry->tn_type != VREG || 857c478bd9Sstevel@tonic-gate tmp_taccess(entry, VWRITE, cr) != 0)) 867c478bd9Sstevel@tonic-gate return (secpolicy_vnode_remove(cr)); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate return (0); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* 927c478bd9Sstevel@tonic-gate * Allocate zeroed memory if tmpfs_maxkmem has not been exceeded 937c478bd9Sstevel@tonic-gate * or the 'musthave' flag is set. 'musthave' allocations should 947c478bd9Sstevel@tonic-gate * always be subordinate to normal allocations so that tmpfs_maxkmem 957c478bd9Sstevel@tonic-gate * can't be exceeded by more than a few KB. Example: when creating 967c478bd9Sstevel@tonic-gate * a new directory, the tmpnode is a normal allocation; if that 977c478bd9Sstevel@tonic-gate * succeeds, the dirents for "." and ".." are 'musthave' allocations. 987c478bd9Sstevel@tonic-gate */ 997c478bd9Sstevel@tonic-gate void * 1007c478bd9Sstevel@tonic-gate tmp_memalloc(size_t size, int musthave) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate static time_t last_warning; 1037c478bd9Sstevel@tonic-gate time_t now; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if (atomic_add_long_nv(&tmp_kmemspace, size) < tmpfs_maxkmem || 1067c478bd9Sstevel@tonic-gate musthave) 1077c478bd9Sstevel@tonic-gate return (kmem_zalloc(size, KM_SLEEP)); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate atomic_add_long(&tmp_kmemspace, -size); 1107c478bd9Sstevel@tonic-gate now = gethrestime_sec(); 1117c478bd9Sstevel@tonic-gate if (last_warning != now) { 1127c478bd9Sstevel@tonic-gate last_warning = now; 1137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "tmp_memalloc: tmpfs over memory limit"); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate return (NULL); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate void 1197c478bd9Sstevel@tonic-gate tmp_memfree(void *cp, size_t size) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate kmem_free(cp, size); 1227c478bd9Sstevel@tonic-gate atomic_add_long(&tmp_kmemspace, -size); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Convert a string containing a number (number of bytes) to a pgcnt_t, 1277c478bd9Sstevel@tonic-gate * containing the corresponding number of pages. On 32-bit kernels, the 1287c478bd9Sstevel@tonic-gate * maximum value encoded in 'str' is PAGESIZE * ULONG_MAX, while the value 1297c478bd9Sstevel@tonic-gate * returned in 'maxpg' is at most ULONG_MAX. 1307c478bd9Sstevel@tonic-gate * 1317c478bd9Sstevel@tonic-gate * If the number is followed by a "k" or "K", the value is converted from 1327c478bd9Sstevel@tonic-gate * kilobytes to bytes. If it is followed by an "m" or "M" it is converted 1337c478bd9Sstevel@tonic-gate * from megabytes to bytes. If it is not followed by a character it is 1347c478bd9Sstevel@tonic-gate * assumed to be in bytes. Multiple letter options are allowed, so for instance 1357c478bd9Sstevel@tonic-gate * '2mk' is interpreted as 2gb. 1367c478bd9Sstevel@tonic-gate * 1377c478bd9Sstevel@tonic-gate * Parse and overflow errors are detected and a non-zero number returned on 1387c478bd9Sstevel@tonic-gate * error. 1397c478bd9Sstevel@tonic-gate */ 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate int 1427c478bd9Sstevel@tonic-gate tmp_convnum(char *str, pgcnt_t *maxpg) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate uint64_t num = 0, oldnum; 1457c478bd9Sstevel@tonic-gate #ifdef _LP64 1467c478bd9Sstevel@tonic-gate uint64_t max_bytes = ULONG_MAX; 1477c478bd9Sstevel@tonic-gate #else 1487c478bd9Sstevel@tonic-gate uint64_t max_bytes = PAGESIZE * (uint64_t)ULONG_MAX; 1497c478bd9Sstevel@tonic-gate #endif 1507c478bd9Sstevel@tonic-gate char *c; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate if (str == NULL) 1537c478bd9Sstevel@tonic-gate return (EINVAL); 1547c478bd9Sstevel@tonic-gate c = str; 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Convert str to number 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate while ((*c >= '0') && (*c <= '9')) { 1607c478bd9Sstevel@tonic-gate oldnum = num; 1617c478bd9Sstevel@tonic-gate num = num * 10 + (*c++ - '0'); 1627c478bd9Sstevel@tonic-gate if (oldnum > num) /* overflow */ 1637c478bd9Sstevel@tonic-gate return (EINVAL); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* 1677c478bd9Sstevel@tonic-gate * Terminate on null 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate while (*c != '\0') { 1707c478bd9Sstevel@tonic-gate switch (*c++) { 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate /* 1737c478bd9Sstevel@tonic-gate * convert from kilobytes 1747c478bd9Sstevel@tonic-gate */ 1757c478bd9Sstevel@tonic-gate case 'k': 1767c478bd9Sstevel@tonic-gate case 'K': 1777c478bd9Sstevel@tonic-gate if (num > max_bytes / 1024) /* will overflow */ 1787c478bd9Sstevel@tonic-gate return (EINVAL); 1797c478bd9Sstevel@tonic-gate num *= 1024; 1807c478bd9Sstevel@tonic-gate break; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* 1837c478bd9Sstevel@tonic-gate * convert from megabytes 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate case 'm': 1867c478bd9Sstevel@tonic-gate case 'M': 1877c478bd9Sstevel@tonic-gate if (num > max_bytes / (1024 * 1024)) /* will overflow */ 1887c478bd9Sstevel@tonic-gate return (EINVAL); 1897c478bd9Sstevel@tonic-gate num *= 1024 * 1024; 1907c478bd9Sstevel@tonic-gate break; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate default: 1937c478bd9Sstevel@tonic-gate return (EINVAL); 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * Since btopr() rounds up to page granularity, this round-up can 1997c478bd9Sstevel@tonic-gate * cause an overflow only if 'num' is between (max_bytes - PAGESIZE) 2007c478bd9Sstevel@tonic-gate * and (max_bytes). In this case the resulting number is zero, which 2017c478bd9Sstevel@tonic-gate * is what we check for below. 2027c478bd9Sstevel@tonic-gate */ 2037c478bd9Sstevel@tonic-gate if ((*maxpg = (pgcnt_t)btopr(num)) == 0 && num != 0) 2047c478bd9Sstevel@tonic-gate return (EINVAL); 2057c478bd9Sstevel@tonic-gate return (0); 2067c478bd9Sstevel@tonic-gate } 207