ntsync.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ntsync.c - Kernel driver for NT synchronization primitives
  4. *
  5. * Copyright (C) 2024 Elizabeth Figura <zfigura@codeweavers.com>
  6. */
  7. #include <linux/anon_inodes.h>
  8. #include <linux/file.h>
  9. #include <linux/fs.h>
  10. #include <linux/miscdevice.h>
  11. #include <linux/module.h>
  12. #include <linux/overflow.h>
  13. #include <linux/slab.h>
  14. #include <linux/spinlock.h>
  15. #include <uapi/linux/ntsync.h>
  16. #define NTSYNC_NAME "ntsync"
  17. enum ntsync_type {
  18. NTSYNC_TYPE_SEM,
  19. };
  20. /*
  21. * Individual synchronization primitives are represented by
  22. * struct ntsync_obj, and each primitive is backed by a file.
  23. *
  24. * The whole namespace is represented by a struct ntsync_device also
  25. * backed by a file.
  26. *
  27. * Both rely on struct file for reference counting. Individual
  28. * ntsync_obj objects take a reference to the device when created.
  29. */
  30. struct ntsync_obj {
  31. spinlock_t lock;
  32. enum ntsync_type type;
  33. struct file *file;
  34. struct ntsync_device *dev;
  35. /* The following fields are protected by the object lock. */
  36. union {
  37. struct {
  38. __u32 count;
  39. __u32 max;
  40. } sem;
  41. } u;
  42. };
  43. struct ntsync_device {
  44. struct file *file;
  45. };
  46. /*
  47. * Actually change the semaphore state, returning -EOVERFLOW if it is made
  48. * invalid.
  49. */
  50. static int post_sem_state(struct ntsync_obj *sem, __u32 count)
  51. {
  52. __u32 sum;
  53. lockdep_assert_held(&sem->lock);
  54. if (check_add_overflow(sem->u.sem.count, count, &sum) ||
  55. sum > sem->u.sem.max)
  56. return -EOVERFLOW;
  57. sem->u.sem.count = sum;
  58. return 0;
  59. }
  60. static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp)
  61. {
  62. __u32 __user *user_args = argp;
  63. __u32 prev_count;
  64. __u32 args;
  65. int ret;
  66. if (copy_from_user(&args, argp, sizeof(args)))
  67. return -EFAULT;
  68. if (sem->type != NTSYNC_TYPE_SEM)
  69. return -EINVAL;
  70. spin_lock(&sem->lock);
  71. prev_count = sem->u.sem.count;
  72. ret = post_sem_state(sem, args);
  73. spin_unlock(&sem->lock);
  74. if (!ret && put_user(prev_count, user_args))
  75. ret = -EFAULT;
  76. return ret;
  77. }
  78. static int ntsync_obj_release(struct inode *inode, struct file *file)
  79. {
  80. struct ntsync_obj *obj = file->private_data;
  81. fput(obj->dev->file);
  82. kfree(obj);
  83. return 0;
  84. }
  85. static long ntsync_obj_ioctl(struct file *file, unsigned int cmd,
  86. unsigned long parm)
  87. {
  88. struct ntsync_obj *obj = file->private_data;
  89. void __user *argp = (void __user *)parm;
  90. switch (cmd) {
  91. case NTSYNC_IOC_SEM_POST:
  92. return ntsync_sem_post(obj, argp);
  93. default:
  94. return -ENOIOCTLCMD;
  95. }
  96. }
  97. static const struct file_operations ntsync_obj_fops = {
  98. .owner = THIS_MODULE,
  99. .release = ntsync_obj_release,
  100. .unlocked_ioctl = ntsync_obj_ioctl,
  101. .compat_ioctl = compat_ptr_ioctl,
  102. };
  103. static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev,
  104. enum ntsync_type type)
  105. {
  106. struct ntsync_obj *obj;
  107. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  108. if (!obj)
  109. return NULL;
  110. obj->type = type;
  111. obj->dev = dev;
  112. get_file(dev->file);
  113. spin_lock_init(&obj->lock);
  114. return obj;
  115. }
  116. static int ntsync_obj_get_fd(struct ntsync_obj *obj)
  117. {
  118. struct file *file;
  119. int fd;
  120. fd = get_unused_fd_flags(O_CLOEXEC);
  121. if (fd < 0)
  122. return fd;
  123. file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR);
  124. if (IS_ERR(file)) {
  125. put_unused_fd(fd);
  126. return PTR_ERR(file);
  127. }
  128. obj->file = file;
  129. fd_install(fd, file);
  130. return fd;
  131. }
  132. static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
  133. {
  134. struct ntsync_sem_args __user *user_args = argp;
  135. struct ntsync_sem_args args;
  136. struct ntsync_obj *sem;
  137. int fd;
  138. if (copy_from_user(&args, argp, sizeof(args)))
  139. return -EFAULT;
  140. if (args.count > args.max)
  141. return -EINVAL;
  142. sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM);
  143. if (!sem)
  144. return -ENOMEM;
  145. sem->u.sem.count = args.count;
  146. sem->u.sem.max = args.max;
  147. fd = ntsync_obj_get_fd(sem);
  148. if (fd < 0) {
  149. kfree(sem);
  150. return fd;
  151. }
  152. return put_user(fd, &user_args->sem);
  153. }
  154. static int ntsync_char_open(struct inode *inode, struct file *file)
  155. {
  156. struct ntsync_device *dev;
  157. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  158. if (!dev)
  159. return -ENOMEM;
  160. file->private_data = dev;
  161. dev->file = file;
  162. return nonseekable_open(inode, file);
  163. }
  164. static int ntsync_char_release(struct inode *inode, struct file *file)
  165. {
  166. struct ntsync_device *dev = file->private_data;
  167. kfree(dev);
  168. return 0;
  169. }
  170. static long ntsync_char_ioctl(struct file *file, unsigned int cmd,
  171. unsigned long parm)
  172. {
  173. struct ntsync_device *dev = file->private_data;
  174. void __user *argp = (void __user *)parm;
  175. switch (cmd) {
  176. case NTSYNC_IOC_CREATE_SEM:
  177. return ntsync_create_sem(dev, argp);
  178. default:
  179. return -ENOIOCTLCMD;
  180. }
  181. }
  182. static const struct file_operations ntsync_fops = {
  183. .owner = THIS_MODULE,
  184. .open = ntsync_char_open,
  185. .release = ntsync_char_release,
  186. .unlocked_ioctl = ntsync_char_ioctl,
  187. .compat_ioctl = compat_ptr_ioctl,
  188. };
  189. static struct miscdevice ntsync_misc = {
  190. .minor = MISC_DYNAMIC_MINOR,
  191. .name = NTSYNC_NAME,
  192. .fops = &ntsync_fops,
  193. };
  194. module_misc_device(ntsync_misc);
  195. MODULE_AUTHOR("Elizabeth Figura <zfigura@codeweavers.com>");
  196. MODULE_DESCRIPTION("Kernel driver for NT synchronization primitives");
  197. MODULE_LICENSE("GPL");