]> OCCT Git - occt.git/commitdiff
0017100: [OCC Forum] Buffer overflow vulnerability and loading TKOpenGl without envir...
authorAPL <>
Fri, 10 Feb 2012 09:51:01 +0000 (09:51 +0000)
committerbugmaster <bugmaster@opencascade.com>
Mon, 5 Mar 2012 15:32:25 +0000 (19:32 +0400)
src/Draw/Draw.cxx
src/Graphic3d/Graphic3d_GraphicDevice.cdl
src/Graphic3d/Graphic3d_GraphicDevice.cxx

index b9ca5e765e79e232cbe3aaf6cb2a3a315e82c3db..e961bf21d496fb0073196ce72de52a304c422a01 100755 (executable)
@@ -89,15 +89,16 @@ extern console_semaphore_value volatile console_semaphore;
 extern char console_command[1000];
 #endif
 
-static void ReadInitFile(char* filename)
+static void ReadInitFile (const TCollection_AsciiString& theFileName)
 {
+  TCollection_AsciiString aPath = theFileName;
 #ifdef WNT
   if (!Draw_Batch) {
     try {
       OCC_CATCH_SIGNALS
-      for(Standard_Integer i = 0; filename[i] != 0; i++)
-       if(filename[i] == '\\') filename[i] = '/';
-      sprintf(console_command,"source \"%s\"",filename);
+      aPath.ChangeAll ('\\', '/');
+
+      sprintf(console_command, "source \"%s\"", aPath.ToCString());
       console_semaphore = HAS_CONSOLE_COMMAND;
       while (console_semaphore == HAS_CONSOLE_COMMAND)
         Sleep(10);
@@ -108,9 +109,9 @@ static void ReadInitFile(char* filename)
     }
   } else {
 #endif
-    char* com = new char [strlen(filename)+strlen("source ")+2];
-    sprintf(com,"source %s",filename);
-    Draw_Interprete(com);
+    char* com = new char [aPath.Length() + strlen ("source ") + 2];
+    sprintf (com, "source %s", aPath.ToCString());
+    Draw_Interprete (com);
     delete [] com;
 #ifdef WNT
   }
@@ -159,7 +160,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
   // analyze arguments
   // *****************************************************************
   Draw_Batch = Standard_False;
-  char* runfile = NULL;
+  TCollection_AsciiString aRunFile;
   Standard_Integer i;
   Standard_Boolean isInteractiveForced = Standard_False;
 #ifndef WNT
@@ -181,7 +182,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
     } else if (strcasecmp(argv[i],"-f") == 0) { // -f option should be LAST!
       Draw_VirtualWindows = !isInteractiveForced;
       if (++i < argc) {
-        runfile = argv[i];
+        aRunFile = TCollection_AsciiString (argv[i]);
       }
       break;
     }
@@ -199,7 +200,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
       Draw_VirtualWindows = !isInteractiveForced;
       p = strtok(NULL," \t");
       if (p != NULL) {
-        runfile = p;
+        aRunFile = TCollection_AsciiString (p);
       }
       break;
     }
@@ -261,35 +262,32 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
   // read init files
   // *****************************************************************
   // default
-  char* dflt = getenv("DRAWDEFAULT");
-  if (dflt == NULL)
+
+  if (getenv ("DRAWDEFAULT") == NULL)
   {
-    char* casroot = getenv("CASROOT");
-    if (casroot == NULL)
+    if (getenv ("CASROOT") == NULL)
     {
 #ifdef WNT
-         ReadInitFile("ddefault");
+      ReadInitFile ("ddefault");
 #else
-         cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl;
-         cout << "No default file" << endl;
+      cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl;
+      cout << "No default file" << endl;
 #endif
     }
     else
     {
-      char* thedefault =  (char *) malloc (128);
-      thedefault[0] = '\0';
-      strcat(thedefault,casroot);
-      strcat (thedefault,"/src/DrawResources/DrawDefault");
-      ReadInitFile(thedefault);
+      TCollection_AsciiString aDefStr (getenv ("CASROOT"));
+      aDefStr += "/src/DrawResources/DrawDefault";
+      ReadInitFile (aDefStr);
     }
   }
   else
   {
-    ReadInitFile(dflt);
+    ReadInitFile (getenv ("DRAWDEFAULT"));
   }
 
   // pure batch
-  if (runfile) {
+  if (!aRunFile.IsEmpty()) {
     // do not map raise the windows, so test programs are discrete
 #ifndef WNT
     Draw_LowWindows = Standard_True;
@@ -300,7 +298,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
     Draw_LowWindows = Standard_False;
 #endif
 
-    ReadInitFile(runfile);
+    ReadInitFile (aRunFile);
     // provide a clean exit, this is usefull for some analysis tools
 #ifndef WNT
     return;
index c3bc2245e39ee01bc0eb3a5ca574a69fa7510947..282a268339f5ebb79f399cce999d659c2a7af19d 100755 (executable)
@@ -20,7 +20,8 @@ uses
        GraphicDriver   from Aspect,
        Display         from Aspect,
        GraphicDriver   from Graphic3d,
-       TypeOfMapping   from Xw
+       TypeOfMapping   from Xw,
+       AsciiString     from TCollection
 
 raises
 
@@ -62,14 +63,14 @@ is
        ---Level: Internal
        ---Purpose: Sets the GraphicDriver.
 
-       ShrIsDefined ( me;
-                      aShr     : out CString from Standard )
-               returns Boolean from Standard
+       ShrEnvString ( me )
+               returns AsciiString from TCollection
                is private;
        ---Level: Internal
-       ---Purpose: Returns Standard_True if the shared library
-       --          is defined by the environment.
-       --          (variables : CSF_GraphicShr, CSF_Graphic3dLib, GRAPHICHOME)
+       ---Purpose: Returns the environment string for loading shared graphics library.
+       --          The string can be defined in environment by corresponding variables,
+       --      or default value will be used for loading from system library path
+       --          Environment variables : CSF_GraphicShr
 
 fields
 
index 0b62b4b7b9172ae2c2168704bba3c240c10cbbec..1acec330edae7fa5e384b9c78406f3da23e9a72f 100755 (executable)
                        is loading from the current PATH
 
 ************************************************************************/
-#define RIC120302      //GG Add new constructor to pass Display structure
-//                     directly instead string connexion.
-
-#define XDESTROY
-
-#ifndef WNT
+#if (!defined(_WIN32) && !defined(__WIN32__))
 
 #include <stdio.h>
 #include <sys/utsname.h>
@@ -32,6 +27,7 @@
 #include <Graphic3d_GraphicDevice.ixx>
 #include <Graphic3d_GraphicDriver.hxx>
 #include <OSD_Function.hxx>
+#include <OSD_Environment.hxx>
 #include <TCollection_AsciiString.hxx>
 
 #include <Xw_Cextern.hxx>
@@ -73,7 +69,6 @@ Standard_Boolean status ;
 
 }
 
-//RIC120302
 Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay)
   : Xw_GraphicDevice ()
  {
@@ -104,18 +99,11 @@ Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay)
 
   this->InitMaps (connexion,Xw_TOM_READONLY,0,Standard_True);
 }
-//RIC120302
 
 // Destructor
-
-void Graphic3d_GraphicDevice::Destroy () {
-
-#ifdef DESTROY
-       cout << "Graphic3d_GraphicDevice::Destroy ()\n";
-#endif
-
-       MyGraphicDriver->End ();
-
+void Graphic3d_GraphicDevice::Destroy()
+{
+  MyGraphicDriver->End();
 }
 
 // Methods in order
@@ -128,16 +116,11 @@ Handle(Aspect_GraphicDriver) Graphic3d_GraphicDevice::GraphicDriver () const {
 
 void Graphic3d_GraphicDevice::SetGraphicDriver () {
 
-Standard_CString TheShr;
-
-       if (! ShrIsDefined (TheShr)) {
-           Aspect_GraphicDeviceDefinitionError::Raise
-               ("Bad environment, Graphic Library not defined");
-       }
+  TCollection_AsciiString aShr = ShrEnvString ();
 
-OSD_SharedLibrary TheSharedLibrary (TheShr);
+  OSD_SharedLibrary TheSharedLibrary (aShr.ToCString());
 
-Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
+  Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
 
        if (! Result) {
            Aspect_GraphicDeviceDefinitionError::Raise
@@ -148,7 +131,7 @@ Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
 char *tracevalue = NULL;
            tracevalue = (char *)(getenv ("CSF_GraphicTrace"));
            if (tracevalue)
-               cout << "Information : " << TheShr << " loaded\n" << flush;
+               cout << "Information : " << aShr << " loaded\n" << flush;
 
 OSD_Function new_GLGraphicDriver =
            TheSharedLibrary.DlSymb ("MetaGraphicDriverFactory");
@@ -183,46 +166,22 @@ OSD_Function new_GLGraphicDriver =
 
 }
 
-#define BAD(x) (((x) == NULL) || (strlen((x)) <= 0))
-
-Standard_Boolean Graphic3d_GraphicDevice::ShrIsDefined (Standard_CString& aShr) const {
-
-  char *glso, *glul, *pkno;
-  char *glshr, *casroot;
-
-  casroot = getenv("CASROOT");
-  glso = getenv("CSF_GraphicShr");
-  glul = getenv("GRAPHICHOME");
-  pkno = getenv("CSF_Graphic3dLib");
+TCollection_AsciiString Graphic3d_GraphicDevice::ShrEnvString() const
+{
+  OSD_Environment aEnvShr ("CSF_GraphicShr");
+  if (!aEnvShr.Value().IsEmpty())
+  {
+    return aEnvShr.Value();
+  }
 
-  if (! BAD(glso)) {
-    glshr = getenv("CSF_GraphicShr");
-  } else if (! BAD(casroot)) {
-    TCollection_AsciiString buffString(casroot);
-    struct utsname info; 
-    uname (&info);
-    buffString = buffString + "/";
-    buffString = buffString + info.sysname;
+  // load TKOpenGl using default searching mechanisms in system
 #if defined(__hpux) || defined(HPUX)
-    buffString = buffString + "/lib/libTKOpenGl.sl";
-#elif defined(WNT)
-    buffString = buffString + "/TKOpenGl.dll";
+  return TCollection_AsciiString ("libTKOpenGl.sl");
 #elif defined(__APPLE__)
-    buffString = buffString + "/lib/libTKOpenGl.dylib";
+  return TCollection_AsciiString ("libTKOpenGl.dylib");
 #else
-    buffString = buffString + "/lib/libTKOpenGl.so";
+  return TCollection_AsciiString ("libTKOpenGl.so");
 #endif
-    glshr = (char *) malloc (buffString.Length() + 1);
-    memcpy(glshr, buffString.ToCString(), buffString.Length() + 1);
-  } else {
-    aShr = NULL;
-    printf("You have not defined CSF_GraphicShr or CASROOT, aborting...");
-    return Standard_False;
-  }
- aShr = glshr;
- return Standard_True;
 }
-#endif  // WNT
+
+#endif // !WNT