Add various checks to ipmitool_sel.c. Most of the subcommands have check
for number of arguments now.

In addition, 'sel get' and 'sel delete' IDs are checked to be valid 16 bit
numbers. Without the patch, 'sel delete abcd' tried to delete event 0
instead of 0xabcd.

Signed-off-by: Jan Safranek <jsafr...@redhat.com>
---

 lib/ipmi_sel.c |   50 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/lib/ipmi_sel.c b/lib/ipmi_sel.c
index 98757d1..0effca2 100644
--- a/lib/ipmi_sel.c
+++ b/lib/ipmi_sel.c
@@ -36,6 +36,7 @@
 #define __USE_XOPEN /* glibc2 needs this for strptime */
 #include <time.h>
 #include <ctype.h>
+#include <errno.h>
 
 #include <ipmitool/helper.h>
 #include <ipmitool/log.h>
@@ -1977,9 +1978,10 @@ ipmi_sel_delete(struct ipmi_intf * intf, int argc, char 
** argv)
 {
        struct ipmi_rs * rsp;
        struct ipmi_rq req;
-       uint16_t id;
+       unsigned long id;
        uint8_t msg_data[4];
        int rc = 0;
+       char *end;
 
        if (argc == 0 || strncmp(argv[0], "help", 4) == 0) {
                lprintf(LOG_ERR, "usage: delete <id>...<id>\n");
@@ -1996,7 +1998,19 @@ ipmi_sel_delete(struct ipmi_intf * intf, int argc, char 
** argv)
 
        for (; argc != 0; argc--)
        {
-               id = (uint16_t) strtoul(argv[argc-1], NULL, 0);
+               errno = 0;
+               id = strtoul(argv[argc-1], &end, 0);
+               if (errno != 0 || *end != '\0') {
+                       lprintf(LOG_ERR, "Invalid ID: %s\n", argv[argc-1]);
+                       rc = -1;
+                       continue;
+               }
+               if (id > 0xffff) {
+                       lprintf(LOG_ERR, "Too big ID: %s\n", argv[argc-1]);
+                       rc = -1;
+                       continue;
+               }
+
                msg_data[2] = id & 0xff;
                msg_data[3] = id >> 8;
 
@@ -2027,13 +2041,14 @@ ipmi_sel_delete(struct ipmi_intf * intf, int argc, char 
** argv)
 static int
 ipmi_sel_show_entry(struct ipmi_intf * intf, int argc, char ** argv)
 {
-       uint16_t id;
+       unsigned long id;
        int i, oldv;
        struct sel_event_record evt;
        struct sdr_record_list * sdr;
        struct entity_id entity;
        struct sdr_record_list * list, * entry;
        int rc = 0;
+       char *end;
 
        if (argc == 0 || strncmp(argv[0], "help", 4) == 0) {
                lprintf(LOG_ERR, "usage: sel get <id>...<id>");
@@ -2046,12 +2061,23 @@ ipmi_sel_show_entry(struct ipmi_intf * intf, int argc, 
char ** argv)
        }
 
        for (i=0; i<argc; i++) {
-               id = (uint16_t)strtol(argv[i], NULL, 0);
+               errno = 0;
+               id = strtoul(argv[i], &end, 0);
+               if (errno != 0 || *end !='\0') {
+                       lprintf(LOG_ERR, "Invalid ID: %s\n", argv[i]);
+                       rc = -1;
+                       continue;
+               }
+               if (id > 0xffff) {
+                       lprintf(LOG_ERR, "Too big ID: %s\n", argv[i]);
+                       rc = -1;
+                       continue;
+               }
 
                lprintf(LOG_DEBUG, "Looking up SEL entry 0x%x", id);
 
                /* lookup SEL entry based on ID */
-               ipmi_sel_get_std_entry(intf, id, &evt);
+               ipmi_sel_get_std_entry(intf, (uint16_t) id, &evt);
                if (evt.sel_type.standard_type.sensor_num == 0 && 
evt.sel_type.standard_type.sensor_type == 0 && evt.record_type == 0) {
                        lprintf(LOG_WARN, "SEL Entry 0x%x not found", id);
                        rc = -1;
@@ -2127,7 +2153,7 @@ int ipmi_sel_main(struct ipmi_intf * intf, int argc, char 
** argv)
                lprintf(LOG_ERR, "SEL Commands:  "
                                "info clear delete list elist get add time save 
readraw writeraw interpret");
        else if (strncmp(argv[0], "interpret", 9) == 0) {
-               if (argc < 4) {
+               if (argc != 4) {
                        lprintf(LOG_NOTICE, "usage: sel interpret iana filename 
format(pps)");
                        return 0;
                }
@@ -2136,35 +2162,35 @@ int ipmi_sel_main(struct ipmi_intf * intf, int argc, 
char ** argv)
        else if (strncmp(argv[0], "info", 4) == 0)
                rc = ipmi_sel_get_info(intf);
        else if (strncmp(argv[0], "save", 4) == 0) {
-               if (argc < 2) {
+               if (argc != 2) {
                        lprintf(LOG_NOTICE, "usage: sel save <filename>");
                        return 0;
                }
                rc = ipmi_sel_save_entries(intf, 0, argv[1]);
        }
        else if (strncmp(argv[0], "add", 3) == 0) {
-               if (argc < 2) {
+               if (argc != 2) {
                        lprintf(LOG_NOTICE, "usage: sel add <filename>");
                        return 0;
                }
                rc = ipmi_sel_add_entries_fromfile(intf, argv[1]);
        }
        else if (strncmp(argv[0], "writeraw", 8) == 0) {
-               if (argc < 2) {
+               if (argc != 2) {
                        lprintf(LOG_NOTICE, "usage: sel writeraw <filename>");
                        return 0;
                }
                rc = ipmi_sel_writeraw(intf, argv[1]);
        }
        else if (strncmp(argv[0], "readraw", 7) == 0) {
-               if (argc < 2) {
+               if (argc != 2) {
                        lprintf(LOG_NOTICE, "usage: sel readraw <filename>");
                        return 0;
                }
                rc = ipmi_sel_readraw(intf, argv[1]);
        }
        else if (strncmp(argv[0], "ereadraw", 8) == 0) {
-               if (argc < 2) {
+               if (argc != 2) {
                        lprintf(LOG_NOTICE, "usage: sel ereadraw <filename>");
                        return 0;
                }
@@ -2234,7 +2260,7 @@ int ipmi_sel_main(struct ipmi_intf * intf, int argc, char 
** argv)
                else if (strncmp(argv[1], "get", 3) == 0)
                        ipmi_sel_get_time(intf);
                else if (strncmp(argv[1], "set", 3) == 0) {
-                       if (argc < 3)
+                       if (argc != 3)
                                lprintf(LOG_ERR, "usage: sel time set 
\"mm/dd/yyyy hh:mm:ss\"");
                        else
                                rc = ipmi_sel_set_time(intf, argv[2]);


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Ipmitool-devel mailing list
Ipmitool-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ipmitool-devel

Reply via email to