blob: 0aba3230d0070ca45285e5075154a352030b59fb [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/ipc/util.h
4 * Copyright (C) 1999 Christoph Rohland
5 *
Christian Kujau624dffc2006-01-15 02:43:54 +01006 * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
Kirill Korotaev73ea4132006-10-02 02:18:20 -07007 * namespaces support. 2006 OpenVZ, SWsoft Inc.
8 * Pavel Emelianov <xemul@openvz.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#ifndef _IPC_UTIL_H
12#define _IPC_UTIL_H
13
Johannes Weiner232086b2009-06-20 02:23:29 +020014#include <linux/unistd.h>
Nadia Derbey023a5352007-10-18 23:40:51 -070015#include <linux/err.h>
Davidlohr Bueso15df03c2017-11-17 15:31:18 -080016#include <linux/ipc_namespace.h>
Nadia Derbey7ca7e562007-10-18 23:40:48 -070017
Eric W. Biedermanf83a3962018-03-22 21:45:50 -050018#define IPCMNI 32768 /* <= MAX_INT limit for ipc arrays (including sysctl changes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define SEQ_MULTIPLIER (IPCMNI)
20
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -070021int sem_init(void);
22int msg_init(void);
Manfred Spraul239521f2014-01-27 17:07:04 -080023void shm_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080025struct ipc_namespace;
Eric W. Biederman03f1fc02018-03-23 00:22:05 -050026struct pid_namespace;
Pavel Emelyanovae5e1b22008-02-08 04:18:22 -080027
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070028#ifdef CONFIG_POSIX_MQUEUE
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070029extern void mq_clear_sbinfo(struct ipc_namespace *ns);
30extern void mq_put_mnt(struct ipc_namespace *ns);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070031#else
Serge E. Hallyn7eafd7c2009-04-06 19:01:10 -070032static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
33static inline void mq_put_mnt(struct ipc_namespace *ns) { }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070034#endif
35
36#ifdef CONFIG_SYSVIPC
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -070037int sem_init_ns(struct ipc_namespace *ns);
38int msg_init_ns(struct ipc_namespace *ns);
39int shm_init_ns(struct ipc_namespace *ns);
Kirill Korotaev73ea4132006-10-02 02:18:20 -070040
41void sem_exit_ns(struct ipc_namespace *ns);
42void msg_exit_ns(struct ipc_namespace *ns);
43void shm_exit_ns(struct ipc_namespace *ns);
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070044#else
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -070045static inline int sem_init_ns(struct ipc_namespace *ns) { return 0; }
46static inline int msg_init_ns(struct ipc_namespace *ns) { return 0; }
47static inline int shm_init_ns(struct ipc_namespace *ns) { return 0; }
Serge E. Hallyn614b84c2009-04-06 19:01:08 -070048
49static inline void sem_exit_ns(struct ipc_namespace *ns) { }
50static inline void msg_exit_ns(struct ipc_namespace *ns) { }
51static inline void shm_exit_ns(struct ipc_namespace *ns) { }
52#endif
Kirill Korotaev73ea4132006-10-02 02:18:20 -070053
Nadia Derbey7748dbf2007-10-18 23:40:49 -070054/*
55 * Structure that holds the parameters needed by the ipc operations
56 * (see after)
57 */
58struct ipc_params {
59 key_t key;
60 int flg;
61 union {
62 size_t size; /* for shared memories */
63 int nsems; /* for semaphores */
64 } u; /* holds the getnew() specific param */
65};
66
67/*
68 * Structure that holds some ipc operations. This structure is used to unify
69 * the calls to sys_msgget(), sys_semget(), sys_shmget()
70 * . routine to call to create a new ipc object. Can be one of newque,
71 * newary, newseg
Nadia Derbeyf4566f02007-10-18 23:40:53 -070072 * . routine to call to check permissions for a new ipc object.
Nadia Derbey7748dbf2007-10-18 23:40:49 -070073 * Can be one of security_msg_associate, security_sem_associate,
74 * security_shm_associate
75 * . routine to call for an extra check if needed
76 */
77struct ipc_ops {
Paul McQuade46c0a8c2014-06-06 14:37:37 -070078 int (*getnew)(struct ipc_namespace *, struct ipc_params *);
79 int (*associate)(struct kern_ipc_perm *, int);
80 int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
Nadia Derbey7748dbf2007-10-18 23:40:49 -070081};
82
Mike Waychisonae781772005-09-06 15:17:09 -070083struct seq_file;
Pierre Peiffered2ddbf2008-02-08 04:18:57 -080084struct ipc_ids;
Cedric Le Goater7d69a1f2007-07-15 23:40:58 -070085
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -070086int ipc_init_ids(struct ipc_ids *);
Mike Waychisonae781772005-09-06 15:17:09 -070087#ifdef CONFIG_PROC_FS
88void __init ipc_init_proc_interface(const char *path, const char *header,
Kirill Korotaev73ea4132006-10-02 02:18:20 -070089 int ids, int (*show)(struct seq_file *, void *));
Eric W. Biederman03f1fc02018-03-23 00:22:05 -050090struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
Mike Waychisonae781772005-09-06 15:17:09 -070091#else
92#define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
93#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Kirill Korotaev73ea4132006-10-02 02:18:20 -070095#define IPC_SEM_IDS 0
96#define IPC_MSG_IDS 1
97#define IPC_SHM_IDS 2
98
Nadia Derbeyce621f52007-10-18 23:40:52 -070099#define ipcid_to_idx(id) ((id) % SEQ_MULTIPLIER)
Stanislav Kinsbursky03f59562013-01-04 15:34:50 -0800100#define ipcid_to_seqx(id) ((id) / SEQ_MULTIPLIER)
Davidlohr Buesodaf948c2014-01-27 17:07:09 -0800101#define IPCID_SEQ_MAX min_t(int, INT_MAX/SEQ_MULTIPLIER, USHRT_MAX)
Nadia Derbeyce621f52007-10-18 23:40:52 -0700102
Davidlohr Buesod9a605e2013-09-11 14:26:24 -0700103/* must be called with ids->rwsem acquired for writing */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700104int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
Nadia Derbey3e148c72007-10-18 23:40:54 -0700105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* must be called with both locks acquired. */
Nadia Derbey7ca7e562007-10-18 23:40:48 -0700107void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Guillaume Knispel0cfb6ae2017-09-08 16:17:55 -0700109/* must be called with both locks acquired. */
110void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
111
Nadia Derbeyf4566f02007-10-18 23:40:53 -0700112/* must be called with ipcp locked */
Serge E. Hallynb0e77592011-03-23 16:43:24 -0700113int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Davidlohr Bueso15df03c2017-11-17 15:31:18 -0800115/**
116 * ipc_get_maxid - get the last assigned id
117 * @ids: ipc identifier set
118 *
119 * Called with ipc_ids.rwsem held for reading.
120 */
121static inline int ipc_get_maxid(struct ipc_ids *ids)
122{
123 if (ids->in_use == 0)
124 return -1;
125
126 if (ids->in_use == IPCMNI)
127 return IPCMNI - 1;
128
129 return ids->max_id;
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
133 * For allocation that need to be freed by RCU.
134 * Objects are reference counted, they start with reference count 1.
135 * getref increases the refcount, the putref call that reduces the recount
136 * to 0 schedules the rcu destruction. Caller must guarantee locking.
Manfred Spraul62b49c92017-07-12 14:35:34 -0700137 *
138 * refcount is initialized by ipc_addid(), before that point call_rcu()
139 * must be used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Manfred Sprauldba4cdd2017-07-12 14:34:41 -0700141int ipc_rcu_getref(struct kern_ipc_perm *ptr);
142void ipc_rcu_putref(struct kern_ipc_perm *ptr,
143 void (*func)(struct rcu_head *head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Nadia Derbey023a5352007-10-18 23:40:51 -0700145struct kern_ipc_perm *ipc_lock(struct ipc_ids *, int);
Davidlohr Bueso55b7ae52015-06-30 14:58:42 -0700146struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
149void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
Eric W. Biederman1efdb692012-02-07 16:54:11 -0800150int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
Davidlohr Bueso444d0f62013-04-30 19:15:24 -0700151struct kern_ipc_perm *ipcctl_pre_down_nolock(struct ipc_namespace *ns,
152 struct ipc_ids *ids, int id, int cmd,
153 struct ipc64_perm *perm, int extra_perm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Eric W. Biederman03f1fc02018-03-23 00:22:05 -0500155static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
156{
157 struct pid *old = *pos;
158 if (old != pid) {
159 *pos = get_pid(pid);
160 put_pid(old);
161 }
162}
163
Will Deaconc1d7e012012-07-30 14:42:46 -0700164#ifndef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
Paul McQuade46c0a8c2014-06-06 14:37:37 -0700165/* On IA-64, we always use the "64-bit version" of the IPC structures. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166# define ipc_parse_version(cmd) IPC_64
167#else
Manfred Spraul239521f2014-01-27 17:07:04 -0800168int ipc_parse_version(int *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif
170
171extern void free_msg(struct msg_msg *msg);
Mathias Krause4e9b45a2013-11-12 15:11:47 -0800172extern struct msg_msg *load_msg(const void __user *src, size_t len);
Stanislav Kinsbursky4a674f32013-01-04 15:34:55 -0800173extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
Mathias Krause4e9b45a2013-11-12 15:11:47 -0800174extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
Nadia Derbey7748dbf2007-10-18 23:40:49 -0700175
Nadia Derbey1b531f22007-10-18 23:40:55 -0700176static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int uid)
Nadia Derbey023a5352007-10-18 23:40:51 -0700177{
Davidlohr Bueso7bb4def2013-04-30 19:15:14 -0700178 return uid / SEQ_MULTIPLIER != ipcp->seq;
Nadia Derbey023a5352007-10-18 23:40:51 -0700179}
180
Davidlohr Bueso1ca70032013-07-08 16:01:10 -0700181static inline void ipc_lock_object(struct kern_ipc_perm *perm)
182{
183 spin_lock(&perm->lock);
184}
185
186static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
187{
188 spin_unlock(&perm->lock);
189}
190
191static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
192{
193 assert_spin_locked(&perm->lock);
194}
195
Nadia Derbey023a5352007-10-18 23:40:51 -0700196static inline void ipc_unlock(struct kern_ipc_perm *perm)
197{
Davidlohr Buesocf9d5d72013-07-08 16:01:11 -0700198 ipc_unlock_object(perm);
Nadia Derbey023a5352007-10-18 23:40:51 -0700199 rcu_read_unlock();
200}
201
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800202/*
203 * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
204 * where the respective ipc_ids.rwsem is not being held down.
205 * Checks whether the ipc object is still around or if it's gone already, as
206 * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
207 * Needs to be called with kern_ipc_perm.lock held -- exception made for one
208 * checkpoint case at sys_semtimedop() as noted in code commentary.
209 */
210static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
211{
Rafael Aquini72a8ff22014-01-27 17:07:02 -0800212 return !perm->deleted;
Rafael Aquini0f3d2b02014-01-27 17:07:01 -0800213}
214
Davidlohr Bueso4d2bff52013-04-30 19:15:19 -0700215struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
Pavel Emelyanovb2d75cd2008-02-08 04:18:54 -0800216int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
Mathias Krauseeb66ec42014-06-06 14:37:36 -0700217 const struct ipc_ops *ops, struct ipc_params *params);
Alexey Dobriyan665c7742009-06-17 16:27:57 -0700218void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
219 void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
Al Viro553f7702017-07-08 22:52:47 -0400220
221#ifdef CONFIG_COMPAT
222#include <linux/compat.h>
223struct compat_ipc_perm {
224 key_t key;
225 __compat_uid_t uid;
226 __compat_gid_t gid;
227 __compat_uid_t cuid;
228 __compat_gid_t cgid;
229 compat_mode_t mode;
230 unsigned short seq;
231};
232
Al Viroc0ebccb2017-07-09 10:03:23 -0400233void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
234void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
235int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
236int get_compat_ipc64_perm(struct ipc64_perm *,
237 struct compat_ipc64_perm __user *);
238
Al Viro553f7702017-07-08 22:52:47 -0400239static inline int compat_ipc_parse_version(int *cmd)
240{
241#ifdef CONFIG_ARCH_WANT_COMPAT_IPC_PARSE_VERSION
242 int version = *cmd & IPC_64;
243 *cmd &= ~IPC_64;
244 return version;
245#else
246 return IPC_64;
247#endif
248}
249#endif
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100250
251/* for __ARCH_WANT_SYS_IPC */
252long ksys_semtimedop(int semid, struct sembuf __user *tsops,
253 unsigned int nsops,
Arnd Bergmann21fc5382018-04-13 13:58:00 +0200254 const struct __kernel_timespec __user *timeout);
Dominik Brodowski69894712018-03-20 19:53:58 +0100255long ksys_semget(key_t key, int nsems, int semflg);
Dominik Brodowskid969c6f2018-03-20 20:00:39 +0100256long ksys_semctl(int semid, int semnum, int cmd, unsigned long arg);
Dominik Brodowski3d656612018-03-20 20:06:04 +0100257long ksys_msgget(key_t key, int msgflg);
Dominik Brodowskie340db52018-03-20 20:15:28 +0100258long ksys_msgctl(int msqid, int cmd, struct msqid_ds __user *buf);
Dominik Brodowski078faac2018-03-20 21:25:57 +0100259long ksys_msgrcv(int msqid, struct msgbuf __user *msgp, size_t msgsz,
260 long msgtyp, int msgflg);
Dominik Brodowski31c213f2018-03-20 21:29:00 +0100261long ksys_msgsnd(int msqid, struct msgbuf __user *msgp, size_t msgsz,
262 int msgflg);
Dominik Brodowski65749e02018-03-20 20:07:53 +0100263long ksys_shmget(key_t key, size_t size, int shmflg);
Dominik Brodowskida1e27442018-03-20 20:09:48 +0100264long ksys_shmdt(char __user *shmaddr);
Dominik Brodowskic84d0792018-03-20 20:12:33 +0100265long ksys_shmctl(int shmid, int cmd, struct shmid_ds __user *buf);
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100266
267/* for CONFIG_ARCH_WANT_OLD_COMPAT_IPC */
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100268long compat_ksys_semtimedop(int semid, struct sembuf __user *tsems,
269 unsigned int nsops,
270 const struct compat_timespec __user *timeout);
Arnd Bergmannb0d17572018-04-13 13:58:23 +0200271#ifdef CONFIG_COMPAT
Dominik Brodowskid969c6f2018-03-20 20:00:39 +0100272long compat_ksys_semctl(int semid, int semnum, int cmd, int arg);
Dominik Brodowskie340db52018-03-20 20:15:28 +0100273long compat_ksys_msgctl(int msqid, int cmd, void __user *uptr);
Dominik Brodowski078faac2018-03-20 21:25:57 +0100274long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
275 compat_long_t msgtyp, int msgflg);
Dominik Brodowski31c213f2018-03-20 21:29:00 +0100276long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
277 compat_ssize_t msgsz, int msgflg);
Dominik Brodowskic84d0792018-03-20 20:12:33 +0100278long compat_ksys_shmctl(int shmid, int cmd, void __user *uptr);
Dominik Brodowski41f4f0e2018-03-20 19:48:14 +0100279#endif /* CONFIG_COMPAT */
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif