From: "Jan Beulich" Subject: [PATCH] linux-2.6.18/xenoprof: consolidate read/write of active/passive domain IDs Date: Tue, 24 Jan 2012 16:44:51 +0000 Message-ID: <4F1EEE13020000780006EB28@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartCAE49613.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartCAE49613.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline This doesn't just fold redundant code, but also fixes the potential for a buffer overrun: While active_domains[] was properly sized (matching the main loop in adomain_write()), passive_domains[] was declared one entry too short. Signed-off-by: Jan Beulich --- a/drivers/oprofile/oprofile_files.c +++ b/drivers/oprofile/oprofile_files.c @@ -126,13 +126,22 @@ static const struct file_operations dump =20 #define TMPBUFSIZE 512 =20 -static unsigned int adomains =3D 0; -static int active_domains[MAX_OPROF_DOMAINS + 1]; -static DEFINE_MUTEX(adom_mutex); +struct domain_data { + unsigned int nr; + int ids[MAX_OPROF_DOMAINS + 1]; + struct mutex mutex; + int (*set)(int[], unsigned int); +}; +#define DEFINE_DOMAIN_DATA(what) \ + struct domain_data what##_domains =3D { \ + .mutex =3D __MUTEX_INITIALIZER(what##_domains.mutex), \ + .set =3D oprofile_set_##what \ + } =20 -static ssize_t adomain_write(struct file * file, char const __user * = buf,=20 - size_t count, loff_t * offset) +static ssize_t domain_write(struct file *filp, char const __user *buf, + size_t count, loff_t *offset) { + struct domain_data *dom =3D filp->private_data; char *tmpbuf; char *startp, *endp; int i; @@ -153,7 +162,7 @@ static ssize_t adomain_write(struct file } tmpbuf[count] =3D 0; =20 - mutex_lock(&adom_mutex); + mutex_lock(&dom->mutex); =20 startp =3D tmpbuf; /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking = */ @@ -163,31 +172,32 @@ static ssize_t adomain_write(struct file break; while (ispunct(*endp) || isspace(*endp)) endp++; - active_domains[i] =3D val; - if (active_domains[i] !=3D val) + dom->ids[i] =3D val; + if (dom->ids[i] !=3D val) /* Overflow, force error below */ i =3D MAX_OPROF_DOMAINS + 1; startp =3D endp; } /* Force error on trailing junk */ - adomains =3D *startp ? MAX_OPROF_DOMAINS + 1 : i; + dom->nr =3D *startp ? MAX_OPROF_DOMAINS + 1 : i; =20 kfree(tmpbuf); =20 - if (adomains > MAX_OPROF_DOMAINS - || oprofile_set_active(active_domains, adomains)) { - adomains =3D 0; + if (dom->nr > MAX_OPROF_DOMAINS + || dom->set(dom->ids, dom->nr)) { + dom->nr =3D 0; retval =3D -EINVAL; } =20 - mutex_unlock(&adom_mutex); + mutex_unlock(&dom->mutex); return retval; } =20 -static ssize_t adomain_read(struct file * file, char __user * buf,=20 - size_t count, loff_t * offset) +static ssize_t domain_read(struct file *filp, char __user *buf, + size_t count, loff_t *offset) { - char * tmpbuf; + struct domain_data *dom =3D filp->private_data; + char *tmpbuf; size_t len; int i; ssize_t retval; @@ -195,18 +205,18 @@ static ssize_t adomain_read(struct file=20 if (!(tmpbuf =3D kmalloc(TMPBUFSIZE, GFP_KERNEL))) return -ENOMEM; =20 - mutex_lock(&adom_mutex); + mutex_lock(&dom->mutex); =20 len =3D 0; - for (i =3D 0; i < adomains; i++) + for (i =3D 0; i < dom->nr; i++) len +=3D snprintf(tmpbuf + len, len < TMPBUFSIZE ? TMPBUFSIZE - len : 0, - "%u ", active_domains[i]); + "%u ", dom->ids[i]); WARN_ON(len > TMPBUFSIZE); if (len !=3D 0 && len <=3D TMPBUFSIZE) tmpbuf[len-1] =3D '\n'; =20 - mutex_unlock(&adom_mutex); + mutex_unlock(&dom->mutex); =20 retval =3D simple_read_from_buffer(buf, count, offset, tmpbuf, = len); =20 @@ -214,103 +224,32 @@ static ssize_t adomain_read(struct file=20 return retval; } =20 +static DEFINE_DOMAIN_DATA(active); =20 -static const struct file_operations active_domain_ops =3D { - .read =3D adomain_read, - .write =3D adomain_write, -}; - -static unsigned int pdomains =3D 0; -static int passive_domains[MAX_OPROF_DOMAINS]; -static DEFINE_MUTEX(pdom_mutex); - -static ssize_t pdomain_write(struct file * file, char const __user * = buf,=20 - size_t count, loff_t * offset) +static int adomain_open(struct inode *inode, struct file *filp) { - char *tmpbuf; - char *startp, *endp; - int i; - unsigned long val; - ssize_t retval =3D count; -=09 - if (*offset) - return -EINVAL;=09 - if (count > TMPBUFSIZE - 1) - return -EINVAL; - - if (!(tmpbuf =3D kmalloc(TMPBUFSIZE, GFP_KERNEL))) - return -ENOMEM; - - if (copy_from_user(tmpbuf, buf, count)) { - kfree(tmpbuf); - return -EFAULT; - } - tmpbuf[count] =3D 0; - - mutex_lock(&pdom_mutex); - - startp =3D tmpbuf; - /* Parse one more than MAX_OPROF_DOMAINS, for easy error checking = */ - for (i =3D 0; i <=3D MAX_OPROF_DOMAINS; i++) { - val =3D simple_strtoul(startp, &endp, 0); - if (endp =3D=3D startp) - break; - while (ispunct(*endp) || isspace(*endp)) - endp++; - passive_domains[i] =3D val; - if (passive_domains[i] !=3D val) - /* Overflow, force error below */ - i =3D MAX_OPROF_DOMAINS + 1; - startp =3D endp; - } - /* Force error on trailing junk */ - pdomains =3D *startp ? MAX_OPROF_DOMAINS + 1 : i; - - kfree(tmpbuf); - - if (pdomains > MAX_OPROF_DOMAINS - || oprofile_set_passive(passive_domains, pdomains)) { - pdomains =3D 0; - retval =3D -EINVAL; - } - - mutex_unlock(&pdom_mutex); - return retval; + filp->private_data =3D &active_domains; + return 0; } =20 -static ssize_t pdomain_read(struct file * file, char __user * buf,=20 - size_t count, loff_t * offset) -{ - char * tmpbuf; - size_t len; - int i; - ssize_t retval; - - if (!(tmpbuf =3D kmalloc(TMPBUFSIZE, GFP_KERNEL))) - return -ENOMEM; - - mutex_lock(&pdom_mutex); - - len =3D 0; - for (i =3D 0; i < pdomains; i++) - len +=3D snprintf(tmpbuf + len, - len < TMPBUFSIZE ? TMPBUFSIZE - len : 0, - "%u ", passive_domains[i]); - WARN_ON(len > TMPBUFSIZE); - if (len !=3D 0 && len <=3D TMPBUFSIZE) - tmpbuf[len-1] =3D '\n'; - - mutex_unlock(&pdom_mutex); +static const struct file_operations active_domain_ops =3D { + .open =3D adomain_open, + .read =3D domain_read, + .write =3D domain_write, +}; =20 - retval =3D simple_read_from_buffer(buf, count, offset, tmpbuf, = len); +static DEFINE_DOMAIN_DATA(passive); =20 - kfree(tmpbuf); - return retval; +static int pdomain_open(struct inode *inode, struct file *filp) +{ + filp->private_data =3D &passive_domains; + return 0; } =20 static const struct file_operations passive_domain_ops =3D { - .read =3D pdomain_read, - .write =3D pdomain_write, + .open =3D pdomain_open, + .read =3D domain_read, + .write =3D domain_write, }; =20 void oprofile_create_files(struct super_block * sb, struct dentry * root) --=__PartCAE49613.0__= Content-Type: text/plain; name="xen-oprofile-files.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="xen-oprofile-files.patch" xenoprof: consolidate read/write of active/passive domain IDs=0A=0AThis = doesn't just fold redundant code, but also fixes the potential for=0Aa = buffer overrun: While active_domains[] was properly sized (matching=0Athe = main loop in adomain_write()), passive_domains[] was declared one=0Aentry = too short.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- = a/drivers/oprofile/oprofile_files.c=0A+++ b/drivers/oprofile/oprofile_files= .c=0A@@ -126,13 +126,22 @@ static const struct file_operations dump=0A =0A = #define TMPBUFSIZE 512=0A =0A-static unsigned int adomains =3D 0;=0A-static= int active_domains[MAX_OPROF_DOMAINS + 1];=0A-static DEFINE_MUTEX(adom_mut= ex);=0A+struct domain_data {=0A+ unsigned int nr;=0A+ int ids[MAX_OPR= OF_DOMAINS + 1];=0A+ struct mutex mutex;=0A+ int (*set)(int[], = unsigned int);=0A+};=0A+#define DEFINE_DOMAIN_DATA(what) \=0A+ struct = domain_data what##_domains =3D { \=0A+ .mutex =3D __MUTEX_INITIALI= ZER(what##_domains.mutex), \=0A+ .set =3D oprofile_set_##wha= t \=0A+ }=0A =0A-static ssize_t adomain_write(struct file * file, char = const __user * buf, =0A- size_t count, loff_t = * offset)=0A+static ssize_t domain_write(struct file *filp, char const = __user *buf,=0A+ size_t count, loff_t = *offset)=0A {=0A+ struct domain_data *dom =3D filp->private_data;=0A = char *tmpbuf;=0A char *startp, *endp;=0A int = i;=0A@@ -153,7 +162,7 @@ static ssize_t adomain_write(struct file=0A = }=0A tmpbuf[count] =3D 0;=0A =0A- mutex_lock(&adom_mutex);=0A+ = mutex_lock(&dom->mutex);=0A =0A startp =3D tmpbuf;=0A /* Parse = one more than MAX_OPROF_DOMAINS, for easy error checking */=0A@@ -163,31 = +172,32 @@ static ssize_t adomain_write(struct file=0A = break;=0A while (ispunct(*endp) || isspace(*endp))=0A = endp++;=0A- active_domains[i] =3D val;=0A- if = (active_domains[i] !=3D val)=0A+ dom->ids[i] =3D val;=0A+ = if (dom->ids[i] !=3D val)=0A /* Overflow, force = error below */=0A i =3D MAX_OPROF_DOMAINS + 1;=0A = startp =3D endp;=0A }=0A /* Force error on trailing junk = */=0A- adomains =3D *startp ? MAX_OPROF_DOMAINS + 1 : i;=0A+ dom->nr = =3D *startp ? MAX_OPROF_DOMAINS + 1 : i;=0A =0A kfree(tmpbuf);=0A = =0A- if (adomains > MAX_OPROF_DOMAINS=0A- || oprofile_set_active(= active_domains, adomains)) {=0A- adomains =3D 0;=0A+ if = (dom->nr > MAX_OPROF_DOMAINS=0A+ || dom->set(dom->ids, = dom->nr)) {=0A+ dom->nr =3D 0;=0A retval =3D = -EINVAL;=0A }=0A =0A- mutex_unlock(&adom_mutex);=0A+ mutex_unloc= k(&dom->mutex);=0A return retval;=0A }=0A =0A-static ssize_t = adomain_read(struct file * file, char __user * buf, =0A- = size_t count, loff_t * offset)=0A+static ssize_t domain_read(struct = file *filp, char __user *buf,=0A+ size_t count, = loff_t *offset)=0A {=0A- char * tmpbuf;=0A+ struct domain_data = *dom =3D filp->private_data;=0A+ char *tmpbuf;=0A size_t = len;=0A int i;=0A ssize_t retval;=0A@@ -195,18 +205,18 @@ = static ssize_t adomain_read(struct file =0A if (!(tmpbuf =3D kmalloc(TM= PBUFSIZE, GFP_KERNEL)))=0A return -ENOMEM;=0A =0A- mutex_lock(= &adom_mutex);=0A+ mutex_lock(&dom->mutex);=0A =0A len =3D = 0;=0A- for (i =3D 0; i < adomains; i++)=0A+ for (i =3D 0; i < dom->nr; = i++)=0A len +=3D snprintf(tmpbuf + len,=0A = len < TMPBUFSIZE ? TMPBUFSIZE - len : 0,=0A- = "%u ", active_domains[i]);=0A+ "%u ", dom->ids[i])= ;=0A WARN_ON(len > TMPBUFSIZE);=0A if (len !=3D 0 && len <=3D = TMPBUFSIZE)=0A tmpbuf[len-1] =3D '\n';=0A =0A- mutex_unlock(&adom_= mutex);=0A+ mutex_unlock(&dom->mutex);=0A =0A retval =3D = simple_read_from_buffer(buf, count, offset, tmpbuf, len);=0A =0A@@ = -214,103 +224,32 @@ static ssize_t adomain_read(struct file =0A = return retval;=0A }=0A =0A+static DEFINE_DOMAIN_DATA(active);=0A =0A-static= const struct file_operations active_domain_ops =3D {=0A- .read = =3D adomain_read,=0A- .write =3D adomain_write,=0A-};=0A-=0A-sta= tic unsigned int pdomains =3D 0;=0A-static int passive_domains[MAX_OPROF_DO= MAINS];=0A-static DEFINE_MUTEX(pdom_mutex);=0A-=0A-static ssize_t = pdomain_write(struct file * file, char const __user * buf, =0A- = size_t count, loff_t * offset)=0A+static int adomain_open(struct = inode *inode, struct file *filp)=0A {=0A- char *tmpbuf;=0A- = char *startp, *endp;=0A- int i;=0A- unsigned long val;=0A- = ssize_t retval =3D count;=0A- =0A- if (*offset)=0A- = return -EINVAL; =0A- if (count > TMPBUFSIZE - 1)=0A- return = -EINVAL;=0A-=0A- if (!(tmpbuf =3D kmalloc(TMPBUFSIZE, GFP_KERNEL)))= =0A- return -ENOMEM;=0A-=0A- if (copy_from_user(tmpbuf, buf, = count)) {=0A- kfree(tmpbuf);=0A- return -EFAULT;=0A-= }=0A- tmpbuf[count] =3D 0;=0A-=0A- mutex_lock(&pdom_mutex);=0A= -=0A- startp =3D tmpbuf;=0A- /* Parse one more than MAX_OPROF_DOMAINS, = for easy error checking */=0A- for (i =3D 0; i <=3D MAX_OPROF_DOMAINS; = i++) {=0A- val =3D simple_strtoul(startp, &endp, 0);=0A- = if (endp =3D=3D startp)=0A- break;=0A- = while (ispunct(*endp) || isspace(*endp))=0A- endp++;=0A-= passive_domains[i] =3D val;=0A- if (passive_domains= [i] !=3D val)=0A- /* Overflow, force error below = */=0A- i =3D MAX_OPROF_DOMAINS + 1;=0A- = startp =3D endp;=0A- }=0A- /* Force error on trailing junk */=0A- = pdomains =3D *startp ? MAX_OPROF_DOMAINS + 1 : i;=0A-=0A- kfree(tmpbu= f);=0A-=0A- if (pdomains > MAX_OPROF_DOMAINS=0A- || oprofile_set= _passive(passive_domains, pdomains)) {=0A- pdomains =3D = 0;=0A- retval =3D -EINVAL;=0A- }=0A-=0A- mutex_unlock(&pdom_= mutex);=0A- return retval;=0A+ filp->private_data =3D &active_domain= s;=0A+ return 0;=0A }=0A =0A-static ssize_t pdomain_read(struct file * = file, char __user * buf, =0A- size_t count, loff_t * = offset)=0A-{=0A- char * tmpbuf;=0A- size_t len;=0A- int i;=0A- = ssize_t retval;=0A-=0A- if (!(tmpbuf =3D kmalloc(TMPBUFSIZE, GFP_KERNEL)))= =0A- return -ENOMEM;=0A-=0A- mutex_lock(&pdom_mutex);=0A-=0A- = len =3D 0;=0A- for (i =3D 0; i < pdomains; i++)=0A- len +=3D = snprintf(tmpbuf + len,=0A- len < TMPBUFSIZE ? = TMPBUFSIZE - len : 0,=0A- "%u ", passive_doma= ins[i]);=0A- WARN_ON(len > TMPBUFSIZE);=0A- if (len !=3D 0 && len <=3D = TMPBUFSIZE)=0A- tmpbuf[len-1] =3D '\n';=0A-=0A- mutex_unlock(&pdom_= mutex);=0A+static const struct file_operations active_domain_ops =3D {=0A+ = .open =3D adomain_open,=0A+ .read =3D domain_read,=0A= + .write =3D domain_write,=0A+};=0A =0A- retval =3D = simple_read_from_buffer(buf, count, offset, tmpbuf, len);=0A+static = DEFINE_DOMAIN_DATA(passive);=0A =0A- kfree(tmpbuf);=0A- return = retval;=0A+static int pdomain_open(struct inode *inode, struct file = *filp)=0A+{=0A+ filp->private_data =3D &passive_domains;=0A+ return = 0;=0A }=0A =0A static const struct file_operations passive_domain_ops =3D = {=0A- .read =3D pdomain_read,=0A- .write =3D = pdomain_write,=0A+ .open =3D pdomain_open,=0A+ .read = =3D domain_read,=0A+ .write =3D domain_write,=0A };=0A =0A = void oprofile_create_files(struct super_block * sb, struct dentry * = root)=0A --=__PartCAE49613.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__PartCAE49613.0__=--