1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-07 18:30:55 +03:00

0017100: [OCC Forum] Buffer overflow vulnerability and loading TKOpenGl without environment variables on unix systems

This commit is contained in:
APL 2012-02-10 09:51:01 +00:00 committed by bugmaster
parent bd754989e8
commit 86be42959e
3 changed files with 57 additions and 99 deletions

View File

@ -89,15 +89,16 @@ extern console_semaphore_value volatile console_semaphore;
extern char console_command[1000]; extern char console_command[1000];
#endif #endif
static void ReadInitFile(char* filename) static void ReadInitFile (const TCollection_AsciiString& theFileName)
{ {
TCollection_AsciiString aPath = theFileName;
#ifdef WNT #ifdef WNT
if (!Draw_Batch) { if (!Draw_Batch) {
try { try {
OCC_CATCH_SIGNALS OCC_CATCH_SIGNALS
for(Standard_Integer i = 0; filename[i] != 0; i++) aPath.ChangeAll ('\\', '/');
if(filename[i] == '\\') filename[i] = '/';
sprintf(console_command,"source \"%s\"",filename); sprintf(console_command, "source \"%s\"", aPath.ToCString());
console_semaphore = HAS_CONSOLE_COMMAND; console_semaphore = HAS_CONSOLE_COMMAND;
while (console_semaphore == HAS_CONSOLE_COMMAND) while (console_semaphore == HAS_CONSOLE_COMMAND)
Sleep(10); Sleep(10);
@ -108,9 +109,9 @@ static void ReadInitFile(char* filename)
} }
} else { } else {
#endif #endif
char* com = new char [strlen(filename)+strlen("source ")+2]; char* com = new char [aPath.Length() + strlen ("source ") + 2];
sprintf(com,"source %s",filename); sprintf (com, "source %s", aPath.ToCString());
Draw_Interprete(com); Draw_Interprete (com);
delete [] com; delete [] com;
#ifdef WNT #ifdef WNT
} }
@ -159,7 +160,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
// analyze arguments // analyze arguments
// ***************************************************************** // *****************************************************************
Draw_Batch = Standard_False; Draw_Batch = Standard_False;
char* runfile = NULL; TCollection_AsciiString aRunFile;
Standard_Integer i; Standard_Integer i;
Standard_Boolean isInteractiveForced = Standard_False; Standard_Boolean isInteractiveForced = Standard_False;
#ifndef WNT #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! } else if (strcasecmp(argv[i],"-f") == 0) { // -f option should be LAST!
Draw_VirtualWindows = !isInteractiveForced; Draw_VirtualWindows = !isInteractiveForced;
if (++i < argc) { if (++i < argc) {
runfile = argv[i]; aRunFile = TCollection_AsciiString (argv[i]);
} }
break; break;
} }
@ -199,7 +200,7 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
Draw_VirtualWindows = !isInteractiveForced; Draw_VirtualWindows = !isInteractiveForced;
p = strtok(NULL," \t"); p = strtok(NULL," \t");
if (p != NULL) { if (p != NULL) {
runfile = p; aRunFile = TCollection_AsciiString (p);
} }
break; break;
} }
@ -261,35 +262,32 @@ void Draw_Appli(Standard_Integer argc, char** argv,const FDraw_InitAppli Draw_In
// read init files // read init files
// ***************************************************************** // *****************************************************************
// default // default
char* dflt = getenv("DRAWDEFAULT");
if (dflt == NULL) if (getenv ("DRAWDEFAULT") == NULL)
{ {
char* casroot = getenv("CASROOT"); if (getenv ("CASROOT") == NULL)
if (casroot == NULL)
{ {
#ifdef WNT #ifdef WNT
ReadInitFile("ddefault"); ReadInitFile ("ddefault");
#else #else
cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl; cout << " the CASROOT variable is mandatory to Run OpenCascade "<< endl;
cout << "No default file" << endl; cout << "No default file" << endl;
#endif #endif
} }
else else
{ {
char* thedefault = (char *) malloc (128); TCollection_AsciiString aDefStr (getenv ("CASROOT"));
thedefault[0] = '\0'; aDefStr += "/src/DrawResources/DrawDefault";
strcat(thedefault,casroot); ReadInitFile (aDefStr);
strcat (thedefault,"/src/DrawResources/DrawDefault");
ReadInitFile(thedefault);
} }
} }
else else
{ {
ReadInitFile(dflt); ReadInitFile (getenv ("DRAWDEFAULT"));
} }
// pure batch // pure batch
if (runfile) { if (!aRunFile.IsEmpty()) {
// do not map raise the windows, so test programs are discrete // do not map raise the windows, so test programs are discrete
#ifndef WNT #ifndef WNT
Draw_LowWindows = Standard_True; 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; Draw_LowWindows = Standard_False;
#endif #endif
ReadInitFile(runfile); ReadInitFile (aRunFile);
// provide a clean exit, this is usefull for some analysis tools // provide a clean exit, this is usefull for some analysis tools
#ifndef WNT #ifndef WNT
return; return;

View File

@ -20,7 +20,8 @@ uses
GraphicDriver from Aspect, GraphicDriver from Aspect,
Display from Aspect, Display from Aspect,
GraphicDriver from Graphic3d, GraphicDriver from Graphic3d,
TypeOfMapping from Xw TypeOfMapping from Xw,
AsciiString from TCollection
raises raises
@ -62,14 +63,14 @@ is
---Level: Internal ---Level: Internal
---Purpose: Sets the GraphicDriver. ---Purpose: Sets the GraphicDriver.
ShrIsDefined ( me; ShrEnvString ( me )
aShr : out CString from Standard ) returns AsciiString from TCollection
returns Boolean from Standard
is private; is private;
---Level: Internal ---Level: Internal
---Purpose: Returns Standard_True if the shared library ---Purpose: Returns the environment string for loading shared graphics library.
-- is defined by the environment. -- The string can be defined in environment by corresponding variables,
-- (variables : CSF_GraphicShr, CSF_Graphic3dLib, GRAPHICHOME) -- or default value will be used for loading from system library path
-- Environment variables : CSF_GraphicShr
fields fields

View File

@ -14,12 +14,7 @@
is loading from the current PATH is loading from the current PATH
************************************************************************/ ************************************************************************/
#define RIC120302 //GG Add new constructor to pass Display structure #if (!defined(_WIN32) && !defined(__WIN32__))
// directly instead string connexion.
#define XDESTROY
#ifndef WNT
#include <stdio.h> #include <stdio.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@ -32,6 +27,7 @@
#include <Graphic3d_GraphicDevice.ixx> #include <Graphic3d_GraphicDevice.ixx>
#include <Graphic3d_GraphicDriver.hxx> #include <Graphic3d_GraphicDriver.hxx>
#include <OSD_Function.hxx> #include <OSD_Function.hxx>
#include <OSD_Environment.hxx>
#include <TCollection_AsciiString.hxx> #include <TCollection_AsciiString.hxx>
#include <Xw_Cextern.hxx> #include <Xw_Cextern.hxx>
@ -73,7 +69,6 @@ Standard_Boolean status ;
} }
//RIC120302
Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay) Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay)
: Xw_GraphicDevice () : Xw_GraphicDevice ()
{ {
@ -104,18 +99,11 @@ Graphic3d_GraphicDevice::Graphic3d_GraphicDevice (const Aspect_Display pdisplay)
this->InitMaps (connexion,Xw_TOM_READONLY,0,Standard_True); this->InitMaps (connexion,Xw_TOM_READONLY,0,Standard_True);
} }
//RIC120302
// Destructor // Destructor
void Graphic3d_GraphicDevice::Destroy()
void Graphic3d_GraphicDevice::Destroy () { {
MyGraphicDriver->End();
#ifdef DESTROY
cout << "Graphic3d_GraphicDevice::Destroy ()\n";
#endif
MyGraphicDriver->End ();
} }
// Methods in order // Methods in order
@ -128,16 +116,11 @@ Handle(Aspect_GraphicDriver) Graphic3d_GraphicDevice::GraphicDriver () const {
void Graphic3d_GraphicDevice::SetGraphicDriver () { void Graphic3d_GraphicDevice::SetGraphicDriver () {
Standard_CString TheShr; TCollection_AsciiString aShr = ShrEnvString ();
if (! ShrIsDefined (TheShr)) { OSD_SharedLibrary TheSharedLibrary (aShr.ToCString());
Aspect_GraphicDeviceDefinitionError::Raise
("Bad environment, Graphic Library not defined");
}
OSD_SharedLibrary TheSharedLibrary (TheShr); Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
if (! Result) { if (! Result) {
Aspect_GraphicDeviceDefinitionError::Raise Aspect_GraphicDeviceDefinitionError::Raise
@ -148,7 +131,7 @@ Standard_Boolean Result = TheSharedLibrary.DlOpen (OSD_RTLD_LAZY);
char *tracevalue = NULL; char *tracevalue = NULL;
tracevalue = (char *)(getenv ("CSF_GraphicTrace")); tracevalue = (char *)(getenv ("CSF_GraphicTrace"));
if (tracevalue) if (tracevalue)
cout << "Information : " << TheShr << " loaded\n" << flush; cout << "Information : " << aShr << " loaded\n" << flush;
OSD_Function new_GLGraphicDriver = OSD_Function new_GLGraphicDriver =
TheSharedLibrary.DlSymb ("MetaGraphicDriverFactory"); TheSharedLibrary.DlSymb ("MetaGraphicDriverFactory");
@ -183,46 +166,22 @@ OSD_Function new_GLGraphicDriver =
} }
#define BAD(x) (((x) == NULL) || (strlen((x)) <= 0)) TCollection_AsciiString Graphic3d_GraphicDevice::ShrEnvString() const
{
Standard_Boolean Graphic3d_GraphicDevice::ShrIsDefined (Standard_CString& aShr) const { OSD_Environment aEnvShr ("CSF_GraphicShr");
if (!aEnvShr.Value().IsEmpty())
char *glso, *glul, *pkno; {
char *glshr, *casroot; return aEnvShr.Value();
casroot = getenv("CASROOT");
glso = getenv("CSF_GraphicShr");
glul = getenv("GRAPHICHOME");
pkno = getenv("CSF_Graphic3dLib");
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;
#if defined(__hpux) || defined(HPUX)
buffString = buffString + "/lib/libTKOpenGl.sl";
#elif defined(WNT)
buffString = buffString + "/TKOpenGl.dll";
#elif defined(__APPLE__)
buffString = buffString + "/lib/libTKOpenGl.dylib";
#else
buffString = buffString + "/lib/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; // load TKOpenGl using default searching mechanisms in system
#if defined(__hpux) || defined(HPUX)
return Standard_True; return TCollection_AsciiString ("libTKOpenGl.sl");
#elif defined(__APPLE__)
return TCollection_AsciiString ("libTKOpenGl.dylib");
#else
return TCollection_AsciiString ("libTKOpenGl.so");
#endif
} }
#endif // WNT
#endif // !WNT