Gitweb:     
http://git.kernel.org/git/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=5e2cb1018a8a583b83d56c80f46507da6f3f2b57
Commit:     5e2cb1018a8a583b83d56c80f46507da6f3f2b57
Parent:     be6c28e62e3a304b74013afab029af2021e1f50d
Author:     Miao Xie <[EMAIL PROTECTED]>
AuthorDate: Wed Feb 6 01:36:53 2008 -0800
Committer:  Linus Torvalds <[EMAIL PROTECTED]>
CommitDate: Wed Feb 6 10:41:03 2008 -0800

    time: fix sysfs_show_{available,current}_clocksources() buffer overflow 
problem
    
    I found that there is a buffer overflow problem in the following code.
    
    Version:    2.6.24-rc2,
    File:               kernel/time/clocksource.c:417-432
    --------------------------------------------------------------------
    static ssize_t
    sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
    {
        struct clocksource *src;
        char *curr = buf;
    
        spin_lock_irq(&clocksource_lock);
        list_for_each_entry(src, &clocksource_list, list) {
                curr += sprintf(curr, "%s ", src->name);
        }
        spin_unlock_irq(&clocksource_lock);
    
        curr += sprintf(curr, "\n");
    
        return curr - buf;
    }
    -----------------------------------------------------------------------
    
    sysfs_show_current_clocksources() also has the same problem though in 
practice
    the size of current clocksource's name won't exceed PAGE_SIZE.
    
    I fix the bug by using snprintf according to the specification of the kernel
    (Version:2.6.24-rc2,File:Documentation/filesystems/sysfs.txt)
    
    Fix sysfs_show_available_clocksources() and 
sysfs_show_current_clocksources()
    buffer overflow problem with snprintf().
    
    Signed-off-by: Miao Xie <[EMAIL PROTECTED]>
    Cc: WANG Cong <[EMAIL PROTECTED]>
    Cc: Thomas Gleixner <[EMAIL PROTECTED]>
    Cc: john stultz <[EMAIL PROTECTED]>
    Signed-off-by: Andrew Morton <[EMAIL PROTECTED]>
    Signed-off-by: Linus Torvalds <[EMAIL PROTECTED]>
---
 kernel/time/clocksource.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index 6e9259a..81afb39 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -363,15 +363,13 @@ void clocksource_unregister(struct clocksource *cs)
 static ssize_t
 sysfs_show_current_clocksources(struct sys_device *dev, char *buf)
 {
-       char *curr = buf;
+       ssize_t count = 0;
 
        spin_lock_irq(&clocksource_lock);
-       curr += sprintf(curr, "%s ", curr_clocksource->name);
+       count = snprintf(buf, PAGE_SIZE, "%s\n", curr_clocksource->name);
        spin_unlock_irq(&clocksource_lock);
 
-       curr += sprintf(curr, "\n");
-
-       return curr - buf;
+       return count;
 }
 
 /**
@@ -439,17 +437,20 @@ static ssize_t
 sysfs_show_available_clocksources(struct sys_device *dev, char *buf)
 {
        struct clocksource *src;
-       char *curr = buf;
+       ssize_t count = 0;
 
        spin_lock_irq(&clocksource_lock);
        list_for_each_entry(src, &clocksource_list, list) {
-               curr += sprintf(curr, "%s ", src->name);
+               count += snprintf(buf + count,
+                                 max((ssize_t)PAGE_SIZE - count, (ssize_t)0),
+                                 "%s ", src->name);
        }
        spin_unlock_irq(&clocksource_lock);
 
-       curr += sprintf(curr, "\n");
+       count += snprintf(buf + count,
+                         max((ssize_t)PAGE_SIZE - count, (ssize_t)0), "\n");
 
-       return curr - buf;
+       return count;
 }
 
 /*
-
To unsubscribe from this list: send the line "unsubscribe git-commits-head" in
the body of a message to [EMAIL PROTECTED]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to