@@ -333,6 +333,9 @@ long strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
long count);
long strnlen_user_nofault(const void __user *unsafe_addr, long count);
+ssize_t getline_from_user(char *dst, size_t dst_size,
+ const char __user *src, size_t src_size);
+
/**
* get_kernel_nofault(): safely attempt to read from a location
* @val: read into this variable
@@ -87,3 +87,40 @@ int check_zeroed_user(const void __user *from, size_t size)
return -EFAULT;
}
EXPORT_SYMBOL(check_zeroed_user);
+
+/**
+ * getline_from_user - Copy a single line from user
+ * @dst: Where to copy the line to
+ * @dst_size: Size of the destination buffer
+ * @src: Where to copy the line from
+ * @src_size: Size of the source user buffer
+ *
+ * Copies a number of characters from given user buffer into the dst buffer.
+ * The number of bytes is limited to the lesser of the sizes of both buffers.
+ * If the copied string contains a newline, its first occurrence is replaced
+ * by a NULL byte in the destination buffer. Otherwise the function ensures
+ * the copied string is NULL-terminated.
+ *
+ * Returns the number of copied bytes or a negative error number on failure.
+ */
+
+ssize_t getline_from_user(char *dst, size_t dst_size,
+ const char __user *src, size_t src_size)
+{
+ size_t size = min_t(size_t, dst_size, src_size);
+ char *c;
+ int ret;
+
+ ret = copy_from_user(dst, src, size);
+ if (ret)
+ return -EFAULT;
+
+ dst[size - 1] = '\0';
+
+ c = strchrnul(dst, '\n');
+ if (*c)
+ *c = '\0';
+
+ return c - dst;
+}
+EXPORT_SYMBOL(getline_from_user);