+ kmemdup-introduce.patch added to -mm tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The patch titled

     kmemdup: introduce

has been added to the -mm tree.  Its filename is

     kmemdup-introduce.patch

See http://www.zip.com.au/~akpm/linux/patches/stuff/added-to-mm.txt to find
out what to do about this

------------------------------------------------------
Subject: kmemdup: introduce
From: Alexey Dobriyan <adobriyan@xxxxxxxxx>

One of idiomatic ways to duplicate a region of memory is

	dst = kmalloc(len, GFP_KERNEL);
	if (!dst)
		return -ENOMEM;
	memcpy(dst, src, len);

which is neat code except a programmer needs to write size twice.  Which
sometimes leads to mistakes.  If len passed to kmalloc is smaller that len
passed to memcpy, it's straight overwrite-beyond-end.  If len passed to
memcpy is smaller than len passed to kmalloc, it's either a) legit
behaviour ;-), or b) cloned buffer will contain garbage in second half.

	[TODO: list of such bugs in Linux, other kernels, userspace]

If programmer is given only one place to play with lengths, I believe, such
mistakes could be avoided.

With kmemdup, the snippet above will be rewritten as:

	dst = kmemdup(src, len, GFP_KERNEL);
	if (!dst)
		return -ENOMEM;

This also leads to smaller code (kzalloc effect).


Signed-off-by: Andrew Morton <akpm@xxxxxxxx>
---

 include/linux/string.h |    1 +
 mm/util.c              |   18 ++++++++++++++++++
 2 files changed, 19 insertions(+)

diff -puN include/linux/string.h~kmemdup-introduce include/linux/string.h
--- a/include/linux/string.h~kmemdup-introduce
+++ a/include/linux/string.h
@@ -99,6 +99,7 @@ extern void * memchr(const void *,int,__
 #endif
 
 extern char *kstrdup(const char *s, gfp_t gfp);
+extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
 
 #ifdef __cplusplus
 }
diff -puN mm/util.c~kmemdup-introduce mm/util.c
--- a/mm/util.c~kmemdup-introduce
+++ a/mm/util.c
@@ -40,6 +40,24 @@ char *kstrdup(const char *s, gfp_t gfp)
 }
 EXPORT_SYMBOL(kstrdup);
 
+/**
+ * kmemdup - duplicate region of memory
+ *
+ * @src: memory region to duplicate
+ * @len: memory region length
+ * @gfp: GFP mask to use
+ */
+void *kmemdup(const void *src, size_t len, gfp_t gfp)
+{
+	void *p;
+
+	p = ____kmalloc(len, gfp);
+	if (p)
+		memcpy(p, src, len);
+	return p;
+}
+EXPORT_SYMBOL(kmemdup);
+
 /*
  * strndup_user - duplicate an existing string from user space
  *
_

Patches currently in -mm which might be from adobriyan@xxxxxxxxx are

optical-proc-ide-media.patch
sh-fix-fpn_start-typo.patch
asus_acpi-fix-proc-files-parsing.patch
asus_acpi-dont-printk-on-writing-garbage-to-proc-files.patch
git-alsa.patch
git-agpgart.patch
git-dvb.patch
config_pm=n-slim-drivers-ieee1394-ohci1394c.patch
git-libata-all.patch
git-net.patch
config_pm=n-slim-drivers-pcmcia.patch
i82092-wire-up-errors-from-pci_register_driver.patch
config_pm=n-slim-drivers-serial-8250_pcic.patch
megaraid-fix-warnings-when-config_proc_fs=n.patch
git-xfs.patch
headers_check-improve-include-regexp.patch
headers_check-clarify-error-message.patch
task_struct-ifdef-missedem-v-ipc.patch
ifdef-blktrace-debugging-fields.patch
tty_ioc-keep-davej-sane.patch
ifdef-quota_read-quota_write.patch
reiserfs-ifdef-xattr_sem.patch
reiserfs-ifdef-acl-stuff-from-inode.patch
fsh-ifdef-security-fields.patch
config_pm=n-slim-drivers-parport-parport_serialc.patch
config_pm=n-slim-sound-oss-tridentc.patch
config_pm=n-slim-sound-oss-cs46xxc.patch
windfarm_smu_satc-simplify-around-i2c_add_driver.patch
cramfs-rewrite-init_cramfs_fs.patch
freevxfs-fix-leak-on-error-path.patch
cramfs-make-cramfs_uncompress_exit-return-void.patch
9p-fix-leak-on-error-path.patch
ban-register_filesystemnull.patch
jbd-use-build_bug_on-in-journal-init.patch
really-ignore-kmem_cache_destroy-return-value.patch
make-kmem_cache_destroy-return-void.patch
documentation-submittingdrivers-minor-update.patch
kmemdup-introduce.patch
kmemdup-some-users.patch
remove-null-check-in-register_nls.patch
make-kmem_cache_destroy-return-void-ecryptfs.patch
config_pm=n-slim-drivers-ide-pci-sc1200c.patch
documentation-ioctl-messtxt-start-tree-wide-ioctl-registry.patch
ioctl-messtxt-xfs-typos.patch

-
To unsubscribe from this list: send the line "unsubscribe mm-commits" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Kernel Newbies FAQ]     [Kernel Archive]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Photo]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]

  Powered by Linux