[openib-general] [PATCH] opensm: fix node_desc.description as string usages

Sasha Khapyorsky sashak at voltaire.com
Sat Oct 28 13:00:53 PDT 2006


node_desc.description buffer is received from the network and should
not be NULL-terminated. In such cases using it as regular string in
functions like printf() leads to segmentation faults. This patch fixes
such usages.

Signed-off-by: Sasha Khapyorsky <sashak at voltaire.com>
---
 osm/opensm/osm_drop_mgr.c  |   26 +++++++++++++++++---------
 osm/opensm/osm_helper.c    |    7 +++++--
 osm/opensm/osm_state_mgr.c |   23 ++++++++++++++++++-----
 3 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/osm/opensm/osm_drop_mgr.c b/osm/opensm/osm_drop_mgr.c
index 5ed320e..a35933d 100644
--- a/osm/opensm/osm_drop_mgr.c
+++ b/osm/opensm/osm_drop_mgr.c
@@ -145,7 +145,6 @@ __osm_drop_mgr_remove_port(
   ib_gid_t port_gid;
   ib_mad_notice_attr_t    notice;
   ib_api_status_t         status;
-  char* p_node_desc;
 
   OSM_LOG_ENTER( p_mgr->p_log, __osm_drop_mgr_remove_port );
 
@@ -306,8 +305,9 @@ __osm_drop_mgr_remove_port(
     p_mcm = (osm_mcm_info_t*)cl_qlist_remove_head( &p_port->mcm_list );
   }
 
-  /* initialize the p_node_desc */
-  p_node_desc = p_port->p_node ? (char*)(p_port->p_node->node_desc.description) : "UNKNOWN"; 
+  /* initialize the p_node - may need to get node_desc later */
+  p_node = p_port->p_node;
+
   osm_port_delete( &p_port );
 
   /* issue a notice - trap 65 */
@@ -341,12 +341,20 @@ __osm_drop_mgr_remove_port(
              ib_get_err_str( status ) );
     goto Exit;
   }
-  osm_log( p_mgr->p_log, OSM_LOG_INFO, 
-           "Removed port with GUID:0x%016" PRIx64 
-           " LID range [0x%X,0x%X] of node:%s\n",
-           cl_ntoh64( port_gid.unicast.interface_id ),
-           min_lid_ho, max_lid_ho, p_node_desc );
-  
+  if (osm_log_is_active( p_mgr->p_log, OSM_LOG_INFO ))
+  {
+    char desc[IB_NODE_DESCRIPTION_SIZE];
+    if (p_node)
+    {
+      memcpy(desc, p_node->node_desc.description, IB_NODE_DESCRIPTION_SIZE);
+      desc[IB_NODE_DESCRIPTION_SIZE] = '\0';
+    }
+    osm_log( p_mgr->p_log, OSM_LOG_INFO,
+             "Removed port with GUID:0x%016" PRIx64
+             " LID range [0x%X,0x%X] of node:%s\n",
+             cl_ntoh64( port_gid.unicast.interface_id ),
+             min_lid_ho, max_lid_ho, p_node ? desc : "UNKNOWN" );
+  }
  Exit:
   OSM_LOG_EXIT( p_mgr->p_log );
 }
diff --git a/osm/opensm/osm_helper.c b/osm/opensm/osm_helper.c
index b06b2f2..100892f 100644
--- a/osm/opensm/osm_helper.c
+++ b/osm/opensm/osm_helper.c
@@ -1039,6 +1039,10 @@ osm_dump_node_record(
 
   if( osm_log_is_active( p_log, log_level ) )
   {
+    char desc[sizeof(p_nr->node_desc.description) + 1];
+    memcpy(desc, p_nr->node_desc.description,
+           sizeof(p_nr->node_desc.description));
+    desc[sizeof(desc) - 1] = '\0';
     osm_log( p_log, log_level,
              "Node Record dump:\n"
              "\t\t\t\tRID\n"
@@ -1074,9 +1078,8 @@ osm_dump_node_record(
              cl_ntoh32( p_ni->revision ),
              ib_node_info_get_local_port_num( p_ni ),
              cl_ntoh32( ib_node_info_get_vendor_id( p_ni )),
-             p_nr->node_desc.description
+             desc
              );
-
   }
 }
 
diff --git a/osm/opensm/osm_state_mgr.c b/osm/opensm/osm_state_mgr.c
index 9c159df..c1e6d01 100644
--- a/osm/opensm/osm_state_mgr.c
+++ b/osm/opensm/osm_state_mgr.c
@@ -1072,6 +1072,7 @@ static void
 osm_topology_file_create(
    IN osm_state_mgr_t * const p_mgr )
 {
+   char desc[IB_NODE_DESCRIPTION_SIZE + 1];
    const osm_node_t *p_node;
    char *file_name;
    FILE *rc;
@@ -1136,6 +1137,10 @@ osm_topology_file_create(
                p_default_physp = p_physp;
             }
 
+            memcpy(desc, p_node->node_desc.description,
+                   IB_NODE_DESCRIPTION_SIZE);
+            desc[IB_NODE_DESCRIPTION_SIZE] = '\0';
+
             fprintf( rc, "{ %s%s Ports:%02X"
                      " SystemGUID:%016" PRIx64
                      " NodeGUID:%016" PRIx64
@@ -1158,7 +1163,7 @@ osm_topology_file_create(
                                 ( &p_node->node_info ) ),
                      cl_ntoh32( p_node->node_info.device_id ),
                      cl_ntoh32( p_node->node_info.revision ),
-                     p_node->node_desc.description,
+                     desc,
                      cl_ntoh16( p_default_physp->port_info.base_lid ),
                      cPort );
 
@@ -1173,6 +1178,9 @@ osm_topology_file_create(
                p_default_physp = p_rphysp;
             }
 
+            memcpy(desc, p_nbnode->node_desc.description,
+                   IB_NODE_DESCRIPTION_SIZE);
+            desc[IB_NODE_DESCRIPTION_SIZE] = '\0';
 
             fprintf( rc, "{ %s%s Ports:%02X"
                      " SystemGUID:%016" PRIx64
@@ -1196,7 +1204,7 @@ osm_topology_file_create(
                                 ( &p_nbnode->node_info ) ),
                      cl_ntoh32( p_nbnode->node_info.device_id ),
                      cl_ntoh32( p_nbnode->node_info.revision ),
-                     p_nbnode->node_desc.description,
+                     desc,
                      cl_ntoh16( p_default_physp->port_info.base_lid ),
                      p_rphysp->port_num );
 
@@ -1645,6 +1653,7 @@ static void
 __osm_state_mgr_report_new_ports(
    IN osm_state_mgr_t * const p_mgr )
 {
+   char desc[IB_NODE_DESCRIPTION_SIZE + 1];
    osm_port_t *p_port;
    ib_gid_t port_gid;
    ib_mad_notice_attr_t notice;
@@ -1693,14 +1702,18 @@ __osm_state_mgr_report_new_ports(
                   ib_get_err_str( status ) );
       }
       osm_port_get_lid_range_ho( p_port, &min_lid_ho, &max_lid_ho );
+      if (p_port->p_node)
+      {
+         memcpy(desc, p_port->p_node->node_desc.description,
+                IB_NODE_DESCRIPTION_SIZE);
+         desc[IB_NODE_DESCRIPTION_SIZE] = '\0';
+      }
       osm_log( p_mgr->p_log, OSM_LOG_INFO,
                "Discovered new port with GUID:0x%016" PRIx64
                " LID range [0x%X,0x%X] of node:%s\n",
                cl_ntoh64( port_gid.unicast.interface_id ),
                min_lid_ho, max_lid_ho,
-               ( p_port->p_node ?
-                 ( char * )( p_port->p_node->node_desc.description ) :
-                 "UNKNOWN" ) );
+               p_port->p_node ? desc : "UNKNOWN" );
 
       p_port =
          ( osm_port_t
-- 
1.4.3.2.g4bf7





More information about the general mailing list