^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/ipc/util.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 1992 Krishna Balasubramanian
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Sep 1997 - Call suser() last after "normal" permission checks so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * get BSD style process accounting right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Occurs in several places in the IPC code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Chris Evans, <chris@ferret.lmh.ox.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Nov 1999 - ipc helper functions, unified SMP locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Manfred Spraul <manfred@colorfullife.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Oct 2002 - One lock per IPC id. RCU ipc_free for lock-free grow_ary().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Mingming Cao <cmm@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Mar 2006 - support for audit of ipc object properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Dustin Kirkland <dustin.kirkland@us.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Jun 2006 - namespaces ssupport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * OpenVZ, SWsoft Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Pavel Emelianov <xemul@openvz.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * General sysv ipc locking scheme:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * rcu_read_lock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * obtain the ipc object (kern_ipc_perm) by looking up the id in an idr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * - perform initial checks (capabilities, auditing and permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * etc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * - perform read-only operations, such as INFO command, that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * do not demand atomicity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * acquire the ipc lock (kern_ipc_perm.lock) through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * ipc_lock_object()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * - perform read-only operations that demand atomicity,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * such as STAT command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * - perform data updates, such as SET, RMID commands and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * mechanism-specific operations (semop/semtimedop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * msgsnd/msgrcv, shmat/shmdt).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * drop the ipc lock, through ipc_unlock_object().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * rcu_read_unlock()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * The ids->rwsem must be taken when:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * - creating, removing and iterating the existing entries in ipc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * identifier sets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * - iterating through files under /proc/sysvipc/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Note that sems have a special fast path that avoids kern_ipc_perm.lock -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * see sem_lock().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #include <linux/shm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <linux/msg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/highuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/nsproxy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/rwsem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/ipc_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/rhashtable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct ipc_proc_iface {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) const char *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int (*show)(struct seq_file *, void *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * ipc_init - initialise ipc subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * The various sysv ipc resources (semaphores, messages and shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * memory) are initialised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * A callback routine is registered into the memory hotplug notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * chain: since msgmni scales to lowmem this callback routine will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * called upon successful memory add / remove to recompute msmgni.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int __init ipc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) proc_mkdir("sysvipc", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) sem_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) msg_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) shm_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) device_initcall(ipc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static const struct rhashtable_params ipc_kht_params = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .head_offset = offsetof(struct kern_ipc_perm, khtnode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .key_offset = offsetof(struct kern_ipc_perm, key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .key_len = sizeof_field(struct kern_ipc_perm, key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .automatic_shrinking = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * ipc_init_ids - initialise ipc identifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Set up the sequence range to use for the ipc identifier range (limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * below ipc_mni) then initialise the keys hashtable and ids idr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) void ipc_init_ids(struct ipc_ids *ids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) ids->in_use = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ids->seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) init_rwsem(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rhashtable_init(&ids->key_ht, &ipc_kht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) idr_init(&ids->ipcs_idr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ids->max_idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ids->last_idx = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #ifdef CONFIG_CHECKPOINT_RESTORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) ids->next_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static const struct proc_ops sysvipc_proc_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * ipc_init_proc_interface - create a proc interface for sysipc types using a seq_file interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @path: Path in procfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @header: Banner to be printed at the beginning of the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @ids: ipc id table to iterate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @show: show routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void __init ipc_init_proc_interface(const char *path, const char *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int ids, int (*show)(struct seq_file *, void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct proc_dir_entry *pde;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct ipc_proc_iface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) iface = kmalloc(sizeof(*iface), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!iface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) iface->path = path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) iface->header = header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) iface->ids = ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) iface->show = show;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pde = proc_create_data(path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) S_IRUGO, /* world readable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) NULL, /* parent dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) &sysvipc_proc_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!pde)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kfree(iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * ipc_findkey - find a key in an ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @key: key to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Returns the locked pointer to the ipc structure if found or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * otherwise. If key is found ipc points to the owning ipc structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * Called with writer ipc_ids.rwsem held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static struct kern_ipc_perm *ipc_findkey(struct ipc_ids *ids, key_t key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct kern_ipc_perm *ipcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ipcp = rhashtable_lookup_fast(&ids->key_ht, &key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) ipc_kht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!ipcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ipc_lock_object(ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return ipcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * Insert new IPC object into idr tree, and set sequence number and id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * in the correct order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Especially:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * - the sequence number must be set before inserting the object into the idr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * because the sequence number is accessed without a lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * - the id can/must be set after inserting the object into the idr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * All accesses must be done after getting kern_ipc_perm.lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * The caller must own kern_ipc_perm.lock.of the new object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * On error, the function returns a (negative) error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * To conserve sequence number space, especially with extended ipc_mni,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * the sequence number is incremented only when the returned ID is less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * the last one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline int ipc_idr_alloc(struct ipc_ids *ids, struct kern_ipc_perm *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int idx, next_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #ifdef CONFIG_CHECKPOINT_RESTORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) next_id = ids->next_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ids->next_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * As soon as a new object is inserted into the idr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * ipc_obtain_object_idr() or ipc_obtain_object_check() can find it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * and the lockless preparations for ipc operations can start.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * This means especially: permission checks, audit calls, allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * of undo structures, ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * Thus the object must be fully initialized, and if something fails,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * then the full tear-down sequence must be followed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * (i.e.: set new->deleted, reduce refcount, call_rcu())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (next_id < 0) { /* !CHECKPOINT_RESTORE or next_id is unset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int max_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) max_idx = max(ids->in_use*3/2, ipc_min_cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) max_idx = min(max_idx, ipc_mni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* allocate the idx, with a NULL struct kern_ipc_perm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) idx = idr_alloc_cyclic(&ids->ipcs_idr, NULL, 0, max_idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (idx >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * idx got allocated successfully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * Now calculate the sequence number and set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * pointer for real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (idx <= ids->last_idx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ids->seq++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (ids->seq >= ipcid_seq_max())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) ids->seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) ids->last_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) new->seq = ids->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* no need for smp_wmb(), this is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * inside idr_replace, as part of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * rcu_assign_pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) idr_replace(&ids->ipcs_idr, new, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) new->seq = ipcid_to_seqx(next_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) idx = idr_alloc(&ids->ipcs_idr, new, ipcid_to_idx(next_id),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 0, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (idx >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) new->id = (new->seq << ipcmni_seq_shift()) + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * ipc_addid - add an ipc identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * @new: new ipc permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * @limit: limit for the number of used ids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Add an entry 'new' to the ipc ids idr. The permissions object is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * initialised and the first free entry is set up and the index assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * is returned. The 'new' entry is returned in a locked state on success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * On failure the entry is not locked and a negative err-code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * The caller must use ipc_rcu_putref() to free the identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * Called with writer ipc_ids.rwsem held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int ipc_addid(struct ipc_ids *ids, struct kern_ipc_perm *new, int limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) kuid_t euid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) kgid_t egid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int idx, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* 1) Initialize the refcount so that ipc_rcu_putref works */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) refcount_set(&new->refcount, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (limit > ipc_mni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) limit = ipc_mni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (ids->in_use >= limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock_init(&new->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) spin_lock(&new->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) current_euid_egid(&euid, &egid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) new->cuid = new->uid = euid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) new->gid = new->cgid = egid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) new->deleted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) idx = ipc_idr_alloc(ids, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (idx >= 0 && new->key != IPC_PRIVATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) err = rhashtable_insert_fast(&ids->key_ht, &new->khtnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ipc_kht_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) idr_remove(&ids->ipcs_idr, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) idx = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (idx < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) new->deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) spin_unlock(&new->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ids->in_use++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (idx > ids->max_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ids->max_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) return idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * ipcget_new - create a new ipc object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * @ns: ipc namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * @ops: the actual creation routine to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * @params: its parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * This routine is called by sys_msgget, sys_semget() and sys_shmget()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * when the key is IPC_PRIVATE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int ipcget_new(struct ipc_namespace *ns, struct ipc_ids *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) const struct ipc_ops *ops, struct ipc_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) down_write(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err = ops->getnew(ns, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) up_write(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * ipc_check_perms - check security and permissions for an ipc object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * @ns: ipc namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * @ipcp: ipc permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * @ops: the actual security routine to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * @params: its parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * This routine is called by sys_msgget(), sys_semget() and sys_shmget()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * when the key is not IPC_PRIVATE and that key already exists in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * ds IDR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * On success, the ipc id is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * It is called with ipc_ids.rwsem and ipcp->lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int ipc_check_perms(struct ipc_namespace *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct kern_ipc_perm *ipcp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) const struct ipc_ops *ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct ipc_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (ipcperms(ns, ipcp, params->flg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) err = ops->associate(ipcp, params->flg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) err = ipcp->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * ipcget_public - get an ipc object or create a new one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * @ns: ipc namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * @ops: the actual creation routine to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * @params: its parameters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * This routine is called by sys_msgget, sys_semget() and sys_shmget()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * when the key is not IPC_PRIVATE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * It adds a new entry if the key is not found and does some permission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * / security checkings if the key is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * On success, the ipc id is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static int ipcget_public(struct ipc_namespace *ns, struct ipc_ids *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) const struct ipc_ops *ops, struct ipc_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct kern_ipc_perm *ipcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int flg = params->flg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * Take the lock as a writer since we are potentially going to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * a new entry + read locks are not "upgradable"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) down_write(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) ipcp = ipc_findkey(ids, params->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (ipcp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* key not used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!(flg & IPC_CREAT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err = ops->getnew(ns, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* ipc object has been locked by ipc_findkey() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (flg & IPC_CREAT && flg & IPC_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (ops->more_checks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) err = ops->more_checks(ipcp, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * ipc_check_perms returns the IPC id on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) err = ipc_check_perms(ns, ipcp, ops, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) ipc_unlock(ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) up_write(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * ipc_kht_remove - remove an ipc from the key hashtable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * @ipcp: ipc perm structure containing the key to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * before this function is called, and remain locked on the exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void ipc_kht_remove(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (ipcp->key != IPC_PRIVATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) WARN_ON_ONCE(rhashtable_remove_fast(&ids->key_ht, &ipcp->khtnode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) ipc_kht_params));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * ipc_rmid - remove an ipc identifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * @ipcp: ipc perm structure containing the identifier to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * before this function is called, and remain locked on the exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) void ipc_rmid(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) int idx = ipcid_to_idx(ipcp->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) WARN_ON_ONCE(idr_remove(&ids->ipcs_idr, idx) != ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) ipc_kht_remove(ids, ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) ids->in_use--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) ipcp->deleted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (unlikely(idx == ids->max_idx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) idx--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (idx == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) } while (!idr_find(&ids->ipcs_idr, idx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) ids->max_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * ipc_set_key_private - switch the key of an existing ipc to IPC_PRIVATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * @ipcp: ipc perm structure containing the key to modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) * ipc_ids.rwsem (as a writer) and the spinlock for this ID are held
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * before this function is called, and remain locked on the exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) void ipc_set_key_private(struct ipc_ids *ids, struct kern_ipc_perm *ipcp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) ipc_kht_remove(ids, ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) ipcp->key = IPC_PRIVATE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) bool ipc_rcu_getref(struct kern_ipc_perm *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return refcount_inc_not_zero(&ptr->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) void ipc_rcu_putref(struct kern_ipc_perm *ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) void (*func)(struct rcu_head *head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (!refcount_dec_and_test(&ptr->refcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) call_rcu(&ptr->rcu, func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * ipcperms - check ipc permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * @ns: ipc namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * @ipcp: ipc permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * @flag: desired permission set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * Check user, group, other permissions for access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * to ipc resources. return 0 if allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * @flag will most probably be 0 or ``S_...UGO`` from <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) kuid_t euid = current_euid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) int requested_mode, granted_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) audit_ipc_obj(ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) requested_mode = (flag >> 6) | (flag >> 3) | flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) granted_mode = ipcp->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (uid_eq(euid, ipcp->cuid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) uid_eq(euid, ipcp->uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) granted_mode >>= 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) else if (in_group_p(ipcp->cgid) || in_group_p(ipcp->gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) granted_mode >>= 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /* is there some bit set in requested_mode but not in granted_mode? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if ((requested_mode & ~granted_mode & 0007) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) !ns_capable(ns->user_ns, CAP_IPC_OWNER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return security_ipc_permission(ipcp, flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * Functions to convert between the kern_ipc_perm structure and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * old/new ipc_perm structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * kernel_to_ipc64_perm - convert kernel ipc permissions to user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * @in: kernel permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * @out: new style ipc permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * Turn the kernel object @in into a set of permissions descriptions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * for returning to userspace (@out).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) out->key = in->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) out->uid = from_kuid_munged(current_user_ns(), in->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) out->gid = from_kgid_munged(current_user_ns(), in->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) out->cuid = from_kuid_munged(current_user_ns(), in->cuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) out->cgid = from_kgid_munged(current_user_ns(), in->cgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) out->mode = in->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) out->seq = in->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) * ipc64_perm_to_ipc_perm - convert new ipc permissions to old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * @in: new style ipc permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * @out: old style ipc permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * Turn the new style permissions object @in into a compatibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * object and store it into the @out pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) out->key = in->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) SET_UID(out->uid, in->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) SET_GID(out->gid, in->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) SET_UID(out->cuid, in->cuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) SET_GID(out->cgid, in->cgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) out->mode = in->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) out->seq = in->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * ipc_obtain_object_idr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * @id: ipc id to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * Look for an id in the ipc ids idr and return associated ipc object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * Call inside the RCU critical section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * The ipc object is *not* locked on exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct kern_ipc_perm *out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int idx = ipcid_to_idx(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) out = idr_find(&ids->ipcs_idr, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (!out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * ipc_obtain_object_check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * @id: ipc id to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * Similar to ipc_obtain_object_idr() but also checks the ipc object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) * Call inside the RCU critical section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * The ipc object is *not* locked on exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) struct kern_ipc_perm *out = ipc_obtain_object_idr(ids, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (IS_ERR(out))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (ipc_checkid(out, id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) return out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) * ipcget - Common sys_*get() code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * @ns: namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * @ops: operations to be called on ipc object creation, permission checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * and further checks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * @params: the parameters needed by the previous operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * Common routine called by sys_msgget(), sys_semget() and sys_shmget().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) const struct ipc_ops *ops, struct ipc_params *params)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (params->key == IPC_PRIVATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return ipcget_new(ns, ids, ops, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) return ipcget_public(ns, ids, ops, params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) * ipc_update_perm - update the permissions of an ipc object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * @in: the permission given as input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * @out: the permission of the ipc to set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) kuid_t uid = make_kuid(current_user_ns(), in->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) kgid_t gid = make_kgid(current_user_ns(), in->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (!uid_valid(uid) || !gid_valid(gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) out->uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) out->gid = gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) out->mode = (out->mode & ~S_IRWXUGO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) | (in->mode & S_IRWXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * ipcctl_obtain_check - retrieve an ipc object and check permissions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * @ns: ipc namespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * @ids: the table of ids where to look for the ipc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * @id: the id of the ipc to retrieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * @cmd: the cmd to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * @perm: the permission to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * @extra_perm: one extra permission parameter used by msq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) * This function does some common audit and permissions check for some IPC_XXX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) * cmd and is called from semctl_down, shmctl_down and msgctl_down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * It:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * - retrieves the ipc object with the given id in the given table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * - performs some audit and permission check, depending on the given cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) * - returns a pointer to the ipc object or otherwise, the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) * error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * Call holding the both the rwsem and the rcu read lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) struct ipc_ids *ids, int id, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) struct ipc64_perm *perm, int extra_perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) kuid_t euid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) int err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct kern_ipc_perm *ipcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ipcp = ipc_obtain_object_check(ids, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) if (IS_ERR(ipcp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) err = PTR_ERR(ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) audit_ipc_obj(ipcp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (cmd == IPC_SET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) audit_ipc_set_perm(extra_perm, perm->uid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) perm->gid, perm->mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) euid = current_euid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (uid_eq(euid, ipcp->cuid) || uid_eq(euid, ipcp->uid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) ns_capable(ns->user_ns, CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) return ipcp; /* successful lookup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) * ipc_parse_version - ipc call version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) * @cmd: pointer to command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * Return IPC_64 for new style IPC and IPC_OLD for old style IPC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) * The @cmd value is turned from an encoding command and version into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) * just the command code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int ipc_parse_version(int *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (*cmd & IPC_64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) *cmd ^= IPC_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return IPC_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) return IPC_OLD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) #endif /* CONFIG_ARCH_WANT_IPC_PARSE_VERSION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct ipc_proc_iter {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct ipc_namespace *ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) struct pid_namespace *pid_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct ipc_proc_iface *iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct pid_namespace *ipc_seq_pid_ns(struct seq_file *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) struct ipc_proc_iter *iter = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) return iter->pid_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) * This routine locks the ipc structure found at least at position pos.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) static struct kern_ipc_perm *sysvipc_find_ipc(struct ipc_ids *ids, loff_t pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) loff_t *new_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) struct kern_ipc_perm *ipc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) int total, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) for (id = 0; id < pos && total < ids->in_use; id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) ipc = idr_find(&ids->ipcs_idr, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if (ipc != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) total++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) ipc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (total >= ids->in_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) for (; pos < ipc_mni; pos++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) ipc = idr_find(&ids->ipcs_idr, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (ipc != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ipc_lock_object(ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) *new_pos = pos + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return ipc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) static void *sysvipc_proc_next(struct seq_file *s, void *it, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) struct ipc_proc_iter *iter = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) struct ipc_proc_iface *iface = iter->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) struct kern_ipc_perm *ipc = it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) /* If we had an ipc id locked before, unlock it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (ipc && ipc != SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) ipc_unlock(ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return sysvipc_find_ipc(&iter->ns->ids[iface->ids], *pos, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * File positions: pos 0 -> header, pos n -> ipc id = n - 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * SeqFile iterator: iterator value locked ipc pointer or SEQ_TOKEN_START.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static void *sysvipc_proc_start(struct seq_file *s, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) struct ipc_proc_iter *iter = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) struct ipc_proc_iface *iface = iter->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) struct ipc_ids *ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) ids = &iter->ns->ids[iface->ids];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * Take the lock - this will be released by the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * call to stop().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) down_read(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) /* pos < 0 is invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (*pos < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /* pos == 0 means header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (*pos == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return SEQ_START_TOKEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) /* Find the (pos-1)th ipc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return sysvipc_find_ipc(ids, *pos - 1, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static void sysvipc_proc_stop(struct seq_file *s, void *it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct kern_ipc_perm *ipc = it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) struct ipc_proc_iter *iter = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct ipc_proc_iface *iface = iter->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct ipc_ids *ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) /* If we had a locked structure, release it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (ipc && ipc != SEQ_START_TOKEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ipc_unlock(ipc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) ids = &iter->ns->ids[iface->ids];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) /* Release the lock we took in start() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) up_read(&ids->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) static int sysvipc_proc_show(struct seq_file *s, void *it)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct ipc_proc_iter *iter = s->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct ipc_proc_iface *iface = iter->iface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (it == SEQ_START_TOKEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) seq_puts(s, iface->header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return iface->show(s, it);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) static const struct seq_operations sysvipc_proc_seqops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) .start = sysvipc_proc_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) .stop = sysvipc_proc_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) .next = sysvipc_proc_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) .show = sysvipc_proc_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) static int sysvipc_proc_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct ipc_proc_iter *iter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) iter = __seq_open_private(file, &sysvipc_proc_seqops, sizeof(*iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (!iter)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) iter->iface = PDE_DATA(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) iter->ns = get_ipc_ns(current->nsproxy->ipc_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) iter->pid_ns = get_pid_ns(task_active_pid_ns(current));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) static int sysvipc_proc_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct seq_file *seq = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) struct ipc_proc_iter *iter = seq->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) put_ipc_ns(iter->ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) put_pid_ns(iter->pid_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) return seq_release_private(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) static const struct proc_ops sysvipc_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) .proc_flags = PROC_ENTRY_PERMANENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) .proc_open = sysvipc_proc_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) .proc_release = sysvipc_proc_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) #endif /* CONFIG_PROC_FS */