Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Fix crash while reading from mapped file
@ 2010-12-10 13:55 Rafal Michalski
  2010-12-10 17:45 ` Luiz Augusto von Dentz
  0 siblings, 1 reply; 10+ messages in thread
From: Rafal Michalski @ 2010-12-10 13:55 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Rafal Michalski

After opening file from /var/lib/bluetooth/<bt_addr>/ and mapping to
memory as it is done in "textfile_foreach" function in textfile.c,
it may crash when size of file is equal to page size
(or it's multiplicity) since "strpbrk" function operates on string
so it expects zero at the end of buffer ("mmap" function zeroes
remaining memory when mapped only for a file which size is not a
multiple of the page size, so in this case "strpbrk" function can't
find null terminating character and goes out of bounds).
This patch provide buffer which contains null terminating character to
avoid crash.
---
 src/textfile.c |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/textfile.c b/src/textfile.c
index 2429cc7..393efb8 100644
--- a/src/textfile.c
+++ b/src/textfile.c
@@ -376,7 +376,7 @@ char *textfile_caseget(const char *pathname, const char *key)
 int textfile_foreach(const char *pathname, textfile_cb func, void *data)
 {
 	struct stat st;
-	char *map, *off, *end, *key, *value;
+	char *map, *off, *end, *key, *value, *buffer = NULL;
 	off_t size; size_t len;
 	int fd, err = 0;
 
@@ -404,6 +404,13 @@ int textfile_foreach(const char *pathname, textfile_cb func, void *data)
 
 	off = map;
 
+	if (!(size % getpagesize())) {
+		buffer = malloc(size + 1);
+		memset(buffer, 0, size + 1);
+		memcpy(buffer, map, size);
+		off = buffer;
+	}
+
 	while (1) {
 		end = strpbrk(off, " ");
 		if (!end) {
@@ -458,6 +465,7 @@ unlock:
 
 close:
 	close(fd);
+	free(buffer);
 	errno = err;
 
 	return 0;
-- 
1.6.3.3


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2010-12-17 13:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-10 13:55 [PATCH] Fix crash while reading from mapped file Rafal Michalski
2010-12-10 17:45 ` Luiz Augusto von Dentz
2010-12-14 15:10   ` Lukasz Pawlik
2010-12-14 15:34     ` Johan Hedberg
2010-12-14 15:41       ` Bastien Nocera
2010-12-14 18:20         ` Lukasz Pawlik
2010-12-15 18:19           ` Anderson Lizardo
2010-12-16  9:28             ` Luiz Augusto von Dentz
2010-12-17  9:29               ` Luiz Augusto von Dentz
2010-12-17 13:25                 ` Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox