^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.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 1999 Christoph Rohland
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * namespaces support. 2006 OpenVZ, SWsoft Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Pavel Emelianov <xemul@openvz.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #ifndef _IPC_UTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define _IPC_UTIL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ipc_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * The IPC ID contains 2 separate numbers - index and sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * By default,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * bits 0-14: index (32k, 15 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * bits 15-30: sequence number (64k, 16 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * When IPCMNI extension mode is turned on, the composition changes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * bits 0-23: index (16M, 24 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * bits 24-30: sequence number (128, 7 bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define IPCMNI_SHIFT 15
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define IPCMNI_EXTEND_SHIFT 24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define IPCMNI_EXTEND_MIN_CYCLE (RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define IPCMNI (1 << IPCMNI_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define IPCMNI_EXTEND (1 << IPCMNI_EXTEND_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #ifdef CONFIG_SYSVIPC_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) extern int ipc_mni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) extern int ipc_mni_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) extern int ipc_min_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define ipcmni_seq_shift() ipc_mni_shift
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #else /* CONFIG_SYSVIPC_SYSCTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define ipc_mni IPCMNI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define ipc_min_cycle ((int)RADIX_TREE_MAP_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define ipcmni_seq_shift() IPCMNI_SHIFT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #endif /* CONFIG_SYSVIPC_SYSCTL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) void sem_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void msg_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void shm_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct ipc_namespace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct pid_namespace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #ifdef CONFIG_POSIX_MQUEUE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) extern void mq_clear_sbinfo(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) extern void mq_put_mnt(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static inline void mq_put_mnt(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #ifdef CONFIG_SYSVIPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void sem_init_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) void msg_init_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) void shm_init_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) void sem_exit_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) void msg_exit_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) void shm_exit_ns(struct ipc_namespace *ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static inline void sem_init_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static inline void msg_init_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static inline void shm_init_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static inline void sem_exit_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static inline void msg_exit_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static inline void shm_exit_ns(struct ipc_namespace *ns) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Structure that holds the parameters needed by the ipc operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * (see after)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct ipc_params {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) key_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int flg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) size_t size; /* for shared memories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int nsems; /* for semaphores */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) } u; /* holds the getnew() specific param */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * Structure that holds some ipc operations. This structure is used to unify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * the calls to sys_msgget(), sys_semget(), sys_shmget()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * . routine to call to create a new ipc object. Can be one of newque,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * newary, newseg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * . routine to call to check permissions for a new ipc object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Can be one of security_msg_associate, security_sem_associate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * security_shm_associate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * . routine to call for an extra check if needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct ipc_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) int (*getnew)(struct ipc_namespace *, struct ipc_params *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int (*associate)(struct kern_ipc_perm *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct seq_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct ipc_ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) void ipc_init_ids(struct ipc_ids *ids);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) void __init ipc_init_proc_interface(const char *path, const char *header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int ids, int (*show)(struct seq_file *, void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #define IPC_SEM_IDS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define IPC_MSG_IDS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define IPC_SHM_IDS 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #define ipcid_to_idx(id) ((id) & IPCMNI_IDX_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define ipcid_to_seqx(id) ((id) >> ipcmni_seq_shift())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define ipcid_seq_max() (INT_MAX >> ipcmni_seq_shift())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* must be called with ids->rwsem acquired for writing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* must be called with both locks acquired. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* must be called with both locks acquired. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* must be called with ipcp locked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * ipc_get_maxidx - get the highest assigned index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * @ids: ipc identifier set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * Called with ipc_ids.rwsem held for reading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static inline int ipc_get_maxidx(struct ipc_ids *ids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ids->in_use == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ids->in_use == ipc_mni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ipc_mni - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return ids->max_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^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) * For allocation that need to be freed by RCU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Objects are reference counted, they start with reference count 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * getref increases the refcount, the putref call that reduces the recount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * to 0 schedules the rcu destruction. Caller must guarantee locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * refcount is initialized by ipc_addid(), before that point call_rcu()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * must be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bool ipc_rcu_getref(struct kern_ipc_perm *ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void ipc_rcu_putref(struct kern_ipc_perm *ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void (*func)(struct rcu_head *head));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct ipc_ids *ids, int id, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct ipc64_perm *perm, int extra_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct pid *old = *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (old != pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) *pos = get_pid(pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) put_pid(old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int ipc_parse_version(int *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) extern void free_msg(struct msg_msg *msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) extern struct msg_msg *load_msg(const void __user *src, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return ipcid_to_seqx(id) != ipcp->seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static inline void ipc_lock_object(struct kern_ipc_perm *perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) spin_lock(&perm->lock);
^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) static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) spin_unlock(&perm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) assert_spin_locked(&perm->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static inline void ipc_unlock(struct kern_ipc_perm *perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ipc_unlock_object(perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * where the respective ipc_ids.rwsem is not being held down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Checks whether the ipc object is still around or if it's gone already, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * Needs to be called with kern_ipc_perm.lock held -- exception made for one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * checkpoint case at sys_semtimedop() as noted in code commentary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return !perm->deleted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const struct ipc_ops *ops, struct ipc_params *params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static inline int sem_check_semmni(struct ipc_namespace *ns) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * Check semmni range [0, ipc_mni]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * semmni is the last element of sem_ctls[4] array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipc_mni))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ? -ERANGE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) struct compat_ipc_perm {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) key_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) __compat_uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) __compat_gid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) __compat_uid_t cuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) __compat_gid_t cgid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) compat_mode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned short seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int get_compat_ipc64_perm(struct ipc64_perm *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) struct compat_ipc64_perm __user *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static inline int compat_ipc_parse_version(int *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int version = *cmd & IPC_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) *cmd &= ~IPC_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) long compat_ksys_old_semctl(int semid, int semnum, int cmd, int arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) compat_long_t msgtyp, int msgflg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) compat_ssize_t msgsz, int msgflg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) #endif