PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/bugSeeding/bugSeedingSupport.C

https://github.com/GoblinInventor/edg4x-rose
C | 1246 lines | 694 code | 261 blank | 291 comment | 154 complexity | 202bae544591129e78bd0e6c5d1be75e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. // This is where we put the supporting code for bug seeding.
  2. #include "rose.h"
  3. using namespace std;
  4. using namespace SageBuilder;
  5. using namespace SageInterface;
  6. #include "bugSeeding.h"
  7. SecurityFlaw::SecurityFlaw()
  8. {
  9. // I think that we will discover that seeding the original input application without
  10. // cloning code fragements will be messy and unanalizable by static analysis tools.
  11. // So the value for seedOriginalCode is FALSE by default.
  12. // seedOriginalCode = false;
  13. }
  14. SecurityFlaw::~SecurityFlaw()
  15. {
  16. // Nothing to do here!
  17. }
  18. // ************************
  19. // Virtual Member Functions
  20. // ************************
  21. // This is a virtual member function
  22. void
  23. SecurityFlaw::detectVunerabilities( SgProject *project )
  24. {
  25. // This is a pure virtual function in the bae class, so it should not be called.
  26. printf ("Error: Base class function called: SecurityFlaw::detectVunerabilities() \n");
  27. ROSE_ASSERT(vulnerabilityKindList.empty() == false);
  28. printf ("In SecurityFlaw::detectVunerabilities(): vulnerabilityKindList.size() = %zu \n",vulnerabilityKindList.size());
  29. // Now iterate over the list
  30. vector<SecurityFlaw::Vulnerability*>::iterator i = vulnerabilityKindList.begin();
  31. while (i != vulnerabilityKindList.end())
  32. {
  33. (*i)->detector(project);
  34. i++;
  35. }
  36. }
  37. // This is a virtual member function
  38. void
  39. SecurityFlaw::defineSearchSpace()
  40. {
  41. printf ("Base class function called: SecurityFlaw::defineSearchSpace() \n");
  42. printf ("For now make this an error... \n");
  43. ROSE_ASSERT(false);
  44. }
  45. // This is a virtual member function
  46. void
  47. SecurityFlaw::codeCloneGeneration( SgProject *project )
  48. {
  49. printf ("Base class function called: SecurityFlaw::codeCloneGeneration() \n");
  50. ROSE_ASSERT(seedKindList.empty() == false);
  51. // These should only be called once, not in a loop over all SecurityFlaw::SeedSecurityFlaw objects.
  52. // For now provide a pointer to the first SecurityFlaw::SeedSecurityFlaw base class.
  53. // But these functions should only be called once to generate all clones on a per
  54. // identified security flaw basis. Any more AST copies shouldbe done to support the
  55. // specific requirements of each SecurityFlaw::SeedSecurityFlaw object (defined mechanism).
  56. vector<SecurityFlaw::SeedSecurityFlaw*>::iterator j = seedKindList.begin();
  57. // Generate clones for security flaw vulnerabilities
  58. CloneVulnerability::makeClones(project,*j);
  59. // This marks all the clones with attributes to associate them to one another.
  60. MarkClones::markVulnerabilitiesInClones(project,*j);
  61. printf ("In SecurityFlaw::codeCloneGeneration(): seedKindList.size() = %zu \n",seedKindList.size());
  62. // Reset the counters used to generate uniquely named functions and variables.
  63. uniqueValueSeeding(true);
  64. // DQ (7/26/2008): Added loop over the list of different seeding approaches.
  65. // while (j != seedKindList.end())
  66. {
  67. // Generate clones for seeding methodologies (additional cloning on each clone generated
  68. // for a security flaw vulnerabilities)
  69. CloneSeedLocation::makeClones(project,*j);
  70. j++;
  71. }
  72. // DQ (7/26/2008): Add comments to the clones so that we can identify them in the generated code.
  73. CommentClones::commentClones(project);
  74. }
  75. // This is a virtual member function
  76. void
  77. SecurityFlaw::seedSecurityFlaws( SgProject *project )
  78. {
  79. printf ("Base class function called: SecurityFlaw::seedSecurityFlaws() \n");
  80. // Note that the container of SeedSecurityFlaw was built in BufferOverFlowSecurityFlaw::codeCloneGeneration()
  81. ROSE_ASSERT (project != NULL);
  82. printf ("In BufferOverFlowSecurityFlaw::seedSecurityFlaws() \n");
  83. ROSE_ASSERT(seedKindList.empty() == false);
  84. // Now iterate over the list
  85. vector<SecurityFlaw::SeedSecurityFlaw*>::iterator j = seedKindList.begin();
  86. while (j != seedKindList.end())
  87. {
  88. // Transform the new statment (the copy)
  89. printf ("In SecurityFlaw::seedSecurityFlaws(): SecurityFlaw::SeedSecurityFlaw = %s \n",(*j)->get_name().c_str());
  90. (*j)->seed(project);
  91. j++;
  92. }
  93. }
  94. // ******************
  95. // Static Member Data
  96. // ******************
  97. // Declaration of static data member (collection of all security flaws).
  98. vector<SecurityFlaw*> SecurityFlaw::securityFlawCollection;
  99. // ***********************
  100. // Static Member Functions
  101. // ***********************
  102. // This is a static member function
  103. void
  104. SecurityFlaw::initialize()
  105. {
  106. // When there is support for many different kinds of security flaws the list will be assembled here!
  107. // Build a BufferOverFlowSecurityFlaw object
  108. BufferOverFlowSecurityFlaw* bufferOverFlowSecurityFlaw = new BufferOverFlowSecurityFlaw();
  109. // Call initialize function
  110. // bufferOverFlowSecurityFlaw->initialize ();
  111. securityFlawCollection.push_back(bufferOverFlowSecurityFlaw);
  112. }
  113. // This is a static member function
  114. void
  115. SecurityFlaw::defineAllSearchSpaces()
  116. {
  117. // Not clear no how to implement this part of the plan.
  118. // For now I am leaving the search space specification out of the implementation.
  119. }
  120. // This is a static member function
  121. void
  122. SecurityFlaw::detectAllVunerabilities( SgProject *project )
  123. {
  124. // Call the member function to annotate the AST where each security flaw vulnerabilities exists.
  125. printf ("In SecurityFlaw::detectAllVunerabilities(): securityFlawCollection.size() = %zu \n",securityFlawCollection.size());
  126. vector<SecurityFlaw*>::iterator i = securityFlawCollection.begin();
  127. while (i != securityFlawCollection.end())
  128. {
  129. (*i)->detectVunerabilities(project);
  130. i++;
  131. }
  132. }
  133. // This is a static member function
  134. void
  135. SecurityFlaw::generationAllClones( SgProject *project )
  136. {
  137. // Reset the counters used to generate uniquely named functions and variables.
  138. // uniqueValueVulnerability(true);
  139. // uniqueValueSeeding(true);
  140. printf ("In BufferOverFlowSecurityFlaw::seedSecurityFlaws() \n");
  141. printf ("In SecurityFlaw::generationAllClones(): securityFlawCollection.size() = %zu \n",securityFlawCollection.size());
  142. vector<SecurityFlaw*>::iterator i = securityFlawCollection.begin();
  143. while (i != securityFlawCollection.end())
  144. {
  145. // Reset the counters used to generate uniquely named functions and variables.
  146. uniqueValueVulnerability(true);
  147. // It is redundant to reset this but reset them (both static functions to generate unique values) together anyway.
  148. uniqueValueSeeding(true);
  149. (*i)->codeCloneGeneration(project);
  150. i++;
  151. }
  152. }
  153. void
  154. SecurityFlaw::seedAllSecurityFlaws( SgProject *project )
  155. {
  156. vector<SecurityFlaw*>::iterator i = securityFlawCollection.begin();
  157. while (i != securityFlawCollection.end())
  158. {
  159. (*i)->seedSecurityFlaws(project);
  160. i++;
  161. }
  162. }
  163. void
  164. SecurityFlaw::resetAttributesAfterASTCloning( SgNode* root )
  165. {
  166. printf ("Calling the traversal to reset the attributes on the AST (root = %p = %s) \n",root,root->class_name().c_str());
  167. ResetSecurityFlawAttributesTraversal treeTraversal;
  168. treeTraversal.traverse (root,preorder);
  169. }
  170. // ***************************
  171. // Supporting Member Functions
  172. // ***************************
  173. void
  174. SecurityFlaw::addComment( SgNode* astNode, string comment )
  175. {
  176. // This function adds a comment before the statement contained by the input IR node's associated statement IR node.
  177. SgStatement* associatedStatement = TransformationSupport::getStatement(astNode);
  178. // printf ("In SecurityFlaw::addComment(astNode = %p): Adding comment to SgStatement = %p \n",astNode,associatedStatement);
  179. SageInterface::attachComment(associatedStatement,comment);
  180. }
  181. int
  182. SecurityFlaw::uniqueValueVulnerability( bool reset )
  183. {
  184. // This function retruns a unique integer value and is used to build names of functions,
  185. // variable, etc. to avoid name collisions.
  186. static int i = 0;
  187. // i++;
  188. if (reset == true)
  189. i = 0;
  190. else
  191. i++;
  192. return i;
  193. }
  194. int
  195. SecurityFlaw::uniqueValueSeeding( bool reset )
  196. {
  197. // This function retruns a unique integer value and is used to build names of functions,
  198. // variable, etc. to avoid name collisions.
  199. static int i = 0;
  200. // i++;
  201. if (reset == true)
  202. i = 0;
  203. else
  204. i++;
  205. return i;
  206. }
  207. // **********************************************************************
  208. // SecurityFlaw::Vulnerability
  209. // **********************************************************************
  210. SecurityFlaw::Vulnerability::Vulnerability()
  211. {
  212. }
  213. SecurityFlaw::Vulnerability::~Vulnerability()
  214. {
  215. }
  216. string
  217. SecurityFlaw::Vulnerability::get_name()
  218. {
  219. return "SecurityFlaw::Vulnerability";
  220. }
  221. string
  222. SecurityFlaw::Vulnerability::get_color()
  223. {
  224. return "blue";
  225. }
  226. void
  227. SecurityFlaw::Vulnerability::associateSeeding ( SecurityFlaw::SeedSecurityFlaw* seedingApproach )
  228. {
  229. associatedSeedingTechniques.insert(seedingApproach);
  230. }
  231. // **********************************************************************
  232. // SecurityFlaw::CloneVulnerabilityTraversal
  233. // **********************************************************************
  234. SecurityFlaw::CloneVulnerability::CloneVulnerabilityTraversal::CloneVulnerabilityTraversal( SeedSecurityFlaw* Xptr )
  235. {
  236. ROSE_ASSERT(Xptr != NULL);
  237. associtedSeedSecurityFlaw = Xptr;
  238. }
  239. SecurityFlaw::CloneVulnerability::CloneVulnerabilityTraversal::~CloneVulnerabilityTraversal()
  240. {
  241. associtedSeedSecurityFlaw = NULL;
  242. }
  243. #if 0
  244. // This code does not appear to compile, I don't know why. I have alternatively
  245. // worked around the problem by specifying the code in the header file since it is trivial.
  246. SecurityFlaw::CloneVulnerability::PrimaryVulnerabilityTraversal(SgNode* node)
  247. {
  248. xNode = node;
  249. }
  250. SecurityFlaw::CloneVulnerability::~PrimaryVulnerabilityTraversal()
  251. {
  252. xNode = NULL;
  253. }
  254. #endif
  255. void
  256. SecurityFlaw::CloneVulnerability::makeClones( SgProject* project, SeedSecurityFlaw* flaw )
  257. {
  258. // Build an AST traversal object
  259. CloneVulnerabilityTraversal treeTraversal(flaw);
  260. InheritedAttribute inheritedAttribute;
  261. // This traverses only the input source file (to traverse all header file
  262. // and the source file call "traverse" instead of "traverseInputFiles").
  263. // treeTraversal.traverseInputFiles (project,preorder);
  264. treeTraversal.traverseInputFiles (project,inheritedAttribute);
  265. }
  266. // void BufferOverFlowSecurityFlaw::CloneVulnerabilityTraversal::visit( SgNode* astNode )
  267. SecurityFlaw::CloneVulnerability::InheritedAttribute
  268. SecurityFlaw::CloneVulnerability::CloneVulnerabilityTraversal::evaluateInheritedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute )
  269. {
  270. // This function calls the seeding traversal on the AST formed by:
  271. // 1) Finding the SecurityVulnerabilityAttribute marker
  272. // 2) Backing up the AST to a specific level of grainularity
  273. // 3) Then removing any clones from subtrees of the original code.
  274. ROSE_ASSERT(astNode != NULL);
  275. AstAttribute* existingAttribute = astNode->getAttribute("SecurityVulnerabilityAttribute");
  276. SecurityVulnerabilityAttribute* securityVulnerabilityAttribute = dynamic_cast<SecurityVulnerabilityAttribute*>(existingAttribute);
  277. // Use the value to code specific types of vulnerabilities
  278. // if (securityVulnerabilityAttribute != NULL && securityVulnerabilityAttribute->get_value() == 5)
  279. if (securityVulnerabilityAttribute != NULL)
  280. {
  281. // This is a marked security flaw, now backup to a position from which to build a copy so
  282. // that we can introduce a version with the seeded security flaw. This is the grainularity
  283. // option for the seeding. We can support many different levels of grainularity, but the
  284. // results appear more useful if one one is selected. It is unclear if more that one will
  285. // ever be useful (more than one level of grainularity gets messy).
  286. ROSE_ASSERT(securityVulnerabilityAttribute->get_securityVulnerabilityNode() == astNode);
  287. ROSE_ASSERT(associtedSeedSecurityFlaw != NULL);
  288. vector<SgNode*> grainularityAxis = associtedSeedSecurityFlaw->grainularityOfSeededCode(astNode);
  289. // Iterate from finest level of grainularity (e.g. SgExpression or SgStatement) to largest
  290. // level of grainularity (e.g. SgFunctionDeclaration or SgFile). This is most often a
  291. // container of size == 1.
  292. printf ("grainularityAxis.size() = %lu \n",grainularityAxis.size());
  293. #if 1
  294. // I think this is the better choice but I am not certain.
  295. vector<SgNode*>::reverse_iterator i = grainularityAxis.rbegin();
  296. while (i != grainularityAxis.rend())
  297. #else
  298. vector<SgNode*>::iterator i = grainularityAxis.begin();
  299. while (i != grainularityAxis.end())
  300. #endif
  301. {
  302. // Put code here to build AST copy and transform the copy!
  303. SgNode* subtree = *i;
  304. printf ("subtree = %p = %s \n",subtree,subtree->class_name().c_str());
  305. SgStatement* subTreeStatement = isSgStatement(subtree);
  306. ROSE_ASSERT(subTreeStatement != NULL);
  307. // Make a copy of the expression used to hold the array size in the array declaration.
  308. SgTreeCopy subTreeCopyHelp;
  309. SgStatement* nearestWholeStatementCopy = isSgStatement(subtree->copy(subTreeCopyHelp));
  310. ROSE_ASSERT(nearestWholeStatementCopy != NULL);
  311. printf ("Calling resetAttributesAfterASTCloning... \n");
  312. resetAttributesAfterASTCloning(nearestWholeStatementCopy);
  313. printf ("DONE: Calling resetAttributesAfterASTCloning... \n");
  314. // Mark the original subtree that is being copied as the original (so that we can optionally permit not seeding the original code).
  315. // Note that we may be visiting this IR node for a second time so it might already have an existing SecurityFlawOriginalSubtreeAttribute.
  316. AstAttribute* originalSubtreeAttribute = new SecurityFlawOriginalSubtreeAttribute(0);
  317. ROSE_ASSERT(originalSubtreeAttribute != NULL);
  318. // AstAttribute* previousOriginalSubtreeAttribute = subTreeStatement->getAttribute("SecurityFlawOriginalSubtreeAttribute");
  319. // if (previousOriginalSubtreeAttribute == NULL)
  320. if (subTreeStatement->attributeExists("SecurityFlawOriginalSubtreeAttribute") == false)
  321. {
  322. subTreeStatement->addNewAttribute("SecurityFlawOriginalSubtreeAttribute",originalSubtreeAttribute);
  323. }
  324. // Mark the copy as a copy built only for seeding security flaws (record the primary flaw in the original AST).
  325. // AstAttribute* cloneSubtreeAttribute = new SeededSecurityFlawCloneAttribute(astNode,subtree);
  326. AstAttribute* cloneSubtreeAttribute = new SeededSecurityFlawCloneAttribute(nearestWholeStatementCopy,subtree);
  327. ROSE_ASSERT(cloneSubtreeAttribute != NULL);
  328. // nearestWholeStatementCopy->addNewAttribute("SeededSecurityFlawCloneAttribute",cloneSubtreeAttribute);
  329. if (nearestWholeStatementCopy->attributeExists("SeededSecurityFlawCloneAttribute") == false)
  330. {
  331. // If the subTreeStatement has been copied previously then it had a SecurityFlawOriginalSubtreeAttribute
  332. // attribute added and we want to remove it from the copy just make.
  333. if (nearestWholeStatementCopy->attributeExists("SecurityFlawOriginalSubtreeAttribute") == true)
  334. {
  335. nearestWholeStatementCopy->removeAttribute("SecurityFlawOriginalSubtreeAttribute");
  336. }
  337. nearestWholeStatementCopy->addNewAttribute("SeededSecurityFlawCloneAttribute",cloneSubtreeAttribute);
  338. // Link from SecurityVulnerabilityAttribute to the clone that was generated to support its seeding.
  339. // securityVulnerabilityAttribute->set_associatedClone(nearestWholeStatementCopy);
  340. // Mark the clone's primary vulnerability (i.e. the reason why we bothered to make the clone).
  341. SgNode* locationOfPrimaryVulnerabilityInClone = NULL;
  342. markPrimaryCloneVulnerability(locationOfPrimaryVulnerabilityInClone,astNode,nearestWholeStatementCopy);
  343. }
  344. // Insert the new statement after the statement with the security vulnerability (at the defined level of grainularity)
  345. if (isSgFunctionDeclaration(nearestWholeStatementCopy) != NULL)
  346. {
  347. // Insert functions at the base of the current scope
  348. subTreeStatement->get_scope()->insertStatementInScope(nearestWholeStatementCopy,false);
  349. }
  350. else
  351. {
  352. // Insert statements after the current statement
  353. subTreeStatement->get_scope()->insert_statement(subTreeStatement,nearestWholeStatementCopy,false);
  354. }
  355. // Note that SgFunctionDeclaration IR nodes have the additional detail of requiring symbols to be added.
  356. SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(nearestWholeStatementCopy);
  357. if (functionDeclaration != NULL)
  358. {
  359. SgName functionName = functionDeclaration->get_name();
  360. functionName += string("_SecurityFlawVulnerability_function_") + StringUtility::numberToString(uniqueValueVulnerability());
  361. functionDeclaration->set_name(functionName);
  362. printf ("functionName = %s \n",functionName.str());
  363. ROSE_ASSERT(functionDeclaration->get_scope() != NULL);
  364. SgFunctionSymbol* functionSymbol = new SgFunctionSymbol(functionDeclaration);
  365. SgName mangledName = functionDeclaration->get_mangled_name();
  366. functionDeclaration->get_scope()->insert_symbol(functionName,functionSymbol);
  367. // functionDeclaration->get_scope()->insert_symbol(mangledName,functionSymbol);
  368. functionSymbol->set_parent(functionDeclaration->get_scope()->get_symbol_table());
  369. // addComment (nearestWholeStatementCopy,"*** NOTE Function Containing Seeded Security Flaw: BufferOverFlowSecurityFlaw ");
  370. // error checking
  371. SgSymbol* local_symbol = functionDeclaration->get_symbol_from_symbol_table();
  372. ROSE_ASSERT(local_symbol != NULL);
  373. }
  374. i++;
  375. }
  376. }
  377. return inheritedAttribute;
  378. }
  379. void
  380. SecurityFlaw::ResetSecurityFlawAttributesTraversal::visit( SgNode* astNode )
  381. {
  382. // When a clone is made, any attributes that it has (e.g. to mark vulnerabilities found
  383. // in earlier stages) are copied, but as a result their pointer values need to be reset.
  384. ROSE_ASSERT(astNode != NULL);
  385. SecurityVulnerabilityAttribute* securityVulnerabilityAttributeInClonedCode = dynamic_cast<SecurityVulnerabilityAttribute*>(astNode->getAttribute("SecurityVulnerabilityAttribute"));
  386. if (securityVulnerabilityAttributeInClonedCode != NULL)
  387. {
  388. ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNode() != astNode);
  389. // Note that the securityVulnerabilityNodeInOriginalCode is stored in the SecurityVulnerabilityAttribute,
  390. // so we have a record of what is the AST node in the original AST, here we reset what the parent node is
  391. // that contains the attribute.
  392. securityVulnerabilityAttributeInClonedCode->set_securityVulnerabilityNode(astNode);
  393. // At this point these are not different.
  394. ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNode() != securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNodeInOriginalCode());
  395. }
  396. PrimarySecurityVulnerabilityForCloneAttribute* primarySecurityVulnerabilityAttributeInClonedCode = dynamic_cast<PrimarySecurityVulnerabilityForCloneAttribute*>(astNode->getAttribute("PrimarySecurityVulnerabilityForCloneAttribute"));
  397. if (primarySecurityVulnerabilityAttributeInClonedCode != NULL)
  398. {
  399. ROSE_ASSERT(primarySecurityVulnerabilityAttributeInClonedCode->get_primarySecurityFlawInClone() != astNode);
  400. primarySecurityVulnerabilityAttributeInClonedCode->set_primarySecurityFlawInClone(astNode);
  401. // printf ("Are there any of these in the AST? \n");
  402. // ROSE_ASSERT(false);
  403. }
  404. }
  405. void
  406. SecurityFlaw::CloneVulnerability::markPrimaryCloneVulnerability( SgNode* primaryNodeInClonedCode, SgNode* primaryNodeInOriginalCode, SgNode* rootOfClone )
  407. {
  408. // Build an AST traversal object
  409. PrimaryVulnerabilityTraversal treeTraversal(primaryNodeInClonedCode,primaryNodeInOriginalCode,rootOfClone);
  410. ROSE_ASSERT(primaryNodeInClonedCode == NULL);
  411. // printf ("primaryNodeInClonedCode = %p = %s \n",primaryNodeInClonedCode,primaryNodeInClonedCode->class_name().c_str());
  412. printf ("primaryNodeInOriginalCode = %p = %s \n",primaryNodeInOriginalCode,primaryNodeInOriginalCode->class_name().c_str());
  413. printf ("rootOfClone = %p = %s \n",rootOfClone,rootOfClone->class_name().c_str());
  414. printf ("treeTraversal.primaryVulnerabilityNodeInOriginalCode = %p = %s \n",treeTraversal.primaryVulnerabilityNodeInOriginalCode,treeTraversal.primaryVulnerabilityNodeInOriginalCode->class_name().c_str());
  415. printf ("treeTraversal.rootOfClone = %p = %s \n",treeTraversal.rootOfClone,treeTraversal.rootOfClone->class_name().c_str());
  416. // This traverses only the input source file (to traverse all header file
  417. // and the source file call "traverse" instead of "traverseInputFiles").
  418. treeTraversal.traverse (rootOfClone,preorder);
  419. }
  420. void
  421. SecurityFlaw::CloneVulnerability::PrimaryVulnerabilityTraversal::visit( SgNode* astNode )
  422. {
  423. ROSE_ASSERT(astNode != NULL);
  424. AstAttribute* attributeInClonedCode = astNode->getAttribute("SecurityVulnerabilityAttribute");
  425. SecurityVulnerabilityAttribute* securityVulnerabilityAttributeInClonedCode = dynamic_cast<SecurityVulnerabilityAttribute*>(attributeInClonedCode);
  426. // Use the value to code specific types of vulnerabilities
  427. if (securityVulnerabilityAttributeInClonedCode != NULL)
  428. {
  429. // SgNode* primaryNodeInOriginalCode = primaryVulnerabilityNodeInOriginalCode;
  430. ROSE_ASSERT(primaryVulnerabilityNodeInOriginalCode != NULL);
  431. // DQ (7/27/2008): Now that we reset the attributes this is false!
  432. // ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNode() != astNode);
  433. printf ("primaryVulnerabilityNodeInOriginalCode = %p = %s \n",primaryVulnerabilityNodeInOriginalCode,primaryVulnerabilityNodeInOriginalCode->class_name().c_str());
  434. // Now get the AstAttribute on the primaryNodeInOriginalCode and see if it is the same, if so then we have identified the primary vulnerability in this clone.
  435. AstAttribute* attributeInOriginalCode = primaryVulnerabilityNodeInOriginalCode->getAttribute("SecurityVulnerabilityAttribute");
  436. SecurityVulnerabilityAttribute* securityVulnerabilityAttributeInOriginalCode = dynamic_cast<SecurityVulnerabilityAttribute*>(attributeInOriginalCode);
  437. ROSE_ASSERT(securityVulnerabilityAttributeInOriginalCode != NULL);
  438. // Since AstAttribute are presently shared this is a way of verifying that we have
  439. // linked the location in the clone with the location in the original source code.
  440. // if (securityVulnerabilityAttributeInOriginalCode == securityVulnerabilityAttributeInClonedCode)
  441. // printf ("Can't test for equality between these now, since they are no longer shared! \n");
  442. // ROSE_ASSERT(false);
  443. if (securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNodeInOriginalCode() == securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNodeInOriginalCode())
  444. {
  445. printf ("***** Found primary vulnerability in clone ***** astNode = %p = %s rootOfClone = %p = %s \n",astNode,astNode->class_name().c_str(),rootOfClone,rootOfClone->class_name().c_str());
  446. // SgStatement* statement = isSgStatement(astNode);
  447. // ROSE_ASSERT(statement != NULL);
  448. // if (astNode == primaryVulnerabilityNodeInClone)
  449. // addComment (astNode,std::string("*** NOTE Primary Node for clone: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer->get_name() );
  450. // Add pointer to root of clone for this security vulnerability to the list
  451. // securityVulnerabilityAttributeInOriginalCode->set_associatedClones(rootOfClone);
  452. // Remove the SecurityVulnerabilityAttribute attribute (this should not call the descructor for the AstAttribute object)
  453. astNode->removeAttribute("SecurityVulnerabilityAttribute");
  454. if (astNode->attributeExists("PrimarySecurityVulnerabilityForCloneAttribute") == false)
  455. {
  456. // AstAttribute* primaryVulnerabilityAttribute = new PrimarySecurityVulnerabilityForCloneAttribute(astNode);
  457. ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer != NULL);
  458. PrimarySecurityVulnerabilityForCloneAttribute* primaryVulnerabilityAttribute =
  459. new PrimarySecurityVulnerabilityForCloneAttribute(astNode,rootOfClone,securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer);
  460. ROSE_ASSERT(primaryVulnerabilityAttribute != NULL);
  461. // astNode->addNewAttribute("SeededSecurityFlawCloneAttribute",primaryVulnerabilityAttribute);
  462. astNode->addNewAttribute("PrimarySecurityVulnerabilityForCloneAttribute",primaryVulnerabilityAttribute);
  463. // Note that this could be added to the constructor arguments to avoid having it to be set explicitly
  464. // (it is now set properly in securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNodeInOriginalCode() ).
  465. ROSE_ASSERT(securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNode() != NULL);
  466. primaryVulnerabilityAttribute->set_primaryVulnerabilityInOriginalCode(securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNode());
  467. // if (astNode == primaryVulnerabilityNodeInClone)
  468. // addComment (astNode,std::string("*** NOTE Primary Node for clone: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer->get_name() );
  469. }
  470. else
  471. {
  472. // We may have to deal with a single IR node having several PrimarySecurityVulnerabilityForCloneAttribute
  473. // associated with different security flaws. But for now I want to make this an error!
  474. printf ("Error: this IR nodes already has a PrimarySecurityVulnerabilityForCloneAttribute \n");
  475. ROSE_ASSERT(false);
  476. }
  477. }
  478. }
  479. }
  480. // **********************************************************************
  481. // SecurityFlaw::CloneSeedLocationTraversal
  482. // **********************************************************************
  483. SecurityFlaw::CloneSeedLocation::CloneSeedLocationTraversal::CloneSeedLocationTraversal( SeedSecurityFlaw* Xptr )
  484. {
  485. ROSE_ASSERT(Xptr != NULL);
  486. associtedSeedSecurityFlaw = Xptr;
  487. }
  488. SecurityFlaw::CloneSeedLocation::CloneSeedLocationTraversal::~CloneSeedLocationTraversal()
  489. {
  490. associtedSeedSecurityFlaw = NULL;
  491. }
  492. void
  493. SecurityFlaw::CloneSeedLocation::makeClones( SgProject* project, SeedSecurityFlaw* flaw )
  494. {
  495. // Build an AST traversal object
  496. CloneSeedLocationTraversal treeTraversal(flaw);
  497. InheritedAttribute inheritedAttribute;
  498. // This traverses only the input source file (to traverse all header file
  499. // and the source file call "traverse" instead of "traverseInputFiles").
  500. // treeTraversal.traverseInputFiles (project,preorder);
  501. treeTraversal.traverseInputFiles (project,inheritedAttribute);
  502. }
  503. void
  504. SecurityFlaw::CloneSeedLocation::markPrimarySeedLocations( SgNode* primaryNodeInClonedCode, SgNode* primaryNodeInOriginalCode, SgNode* rootOfClone )
  505. {
  506. // Build an AST traversal object
  507. PrimarySeedLocationTraversal treeTraversal(primaryNodeInClonedCode,primaryNodeInOriginalCode,rootOfClone);
  508. ROSE_ASSERT(primaryNodeInClonedCode == NULL);
  509. // printf ("primaryNodeInClonedCode = %p = %s \n",primaryNodeInClonedCode,primaryNodeInClonedCode->class_name().c_str());
  510. printf ("primaryNodeInOriginalCode = %p = %s \n",primaryNodeInOriginalCode,primaryNodeInOriginalCode->class_name().c_str());
  511. printf ("rootOfClone = %p = %s \n",rootOfClone,rootOfClone->class_name().c_str());
  512. printf ("treeTraversal.primaryVulnerabilityNodeInOriginalCode = %p = %s \n",treeTraversal.primaryVulnerabilityNodeInOriginalCode,treeTraversal.primaryVulnerabilityNodeInOriginalCode->class_name().c_str());
  513. printf ("treeTraversal.rootOfClone = %p = %s \n",treeTraversal.rootOfClone,treeTraversal.rootOfClone->class_name().c_str());
  514. // This traverses only the input source file (to traverse all header file
  515. // and the source file call "traverse" instead of "traverseInputFiles").
  516. treeTraversal.traverse (rootOfClone,preorder);
  517. }
  518. void
  519. SecurityFlaw::CloneSeedLocation::PrimarySeedLocationTraversal::visit( SgNode* astNode )
  520. {
  521. ROSE_ASSERT(astNode != NULL);
  522. AstAttribute* attributeInClonedCode = astNode->getAttribute("SecurityVulnerabilityAttribute");
  523. SecurityVulnerabilityAttribute* securityVulnerabilityAttributeInClonedCode = dynamic_cast<SecurityVulnerabilityAttribute*>(attributeInClonedCode);
  524. // Use the value to code specific types of vulnerabilities
  525. // if (securityVulnerabilityAttributeInClonedCode != NULL && securityVulnerabilityAttributeInClonedCode->get_value() == 5)
  526. // if (securityVulnerabilityAttributeInClonedCode != NULL && securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNode() == astNode)
  527. if (securityVulnerabilityAttributeInClonedCode != NULL)
  528. {
  529. // SgNode* primaryNodeInOriginalCode = primaryVulnerabilityNodeInOriginalCode;
  530. ROSE_ASSERT(primaryVulnerabilityNodeInOriginalCode != NULL);
  531. ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->get_securityVulnerabilityNode() != astNode);
  532. printf ("primaryVulnerabilityNodeInOriginalCode = %p = %s \n",primaryVulnerabilityNodeInOriginalCode,primaryVulnerabilityNodeInOriginalCode->class_name().c_str());
  533. // Now get the AstAttribute on the primaryNodeInOriginalCode and see if it is the same, if so then we have identified the primary vulnerability in this clone.
  534. AstAttribute* attributeInOriginalCode = primaryVulnerabilityNodeInOriginalCode->getAttribute("SecurityVulnerabilityAttribute");
  535. SecurityVulnerabilityAttribute* securityVulnerabilityAttributeInOriginalCode = dynamic_cast<SecurityVulnerabilityAttribute*>(attributeInOriginalCode);
  536. ROSE_ASSERT(securityVulnerabilityAttributeInOriginalCode != NULL);
  537. // Since AstAttribute are presently shared this is a way of verifying that we have
  538. // linked the location in the clone with the location in the original source code.
  539. printf ("Can't test for equality between these now, since they are no longer shared! \n");
  540. ROSE_ASSERT(false);
  541. if (securityVulnerabilityAttributeInOriginalCode == securityVulnerabilityAttributeInClonedCode)
  542. {
  543. printf ("***** Found primary vulnerability in clone ***** astNode = %p = %s rootOfClone = %p = %s \n",astNode,astNode->class_name().c_str(),rootOfClone,rootOfClone->class_name().c_str());
  544. // SgStatement* statement = isSgStatement(astNode);
  545. // ROSE_ASSERT(statement != NULL);
  546. // if (astNode == primaryVulnerabilityNodeInClone)
  547. // addComment (astNode,std::string("*** NOTE Primary Node for clone: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer->get_name() );
  548. // Add pointer to root of clone for this security vulnerability to the list
  549. // securityVulnerabilityAttributeInOriginalCode->set_associatedClones(rootOfClone);
  550. // Remove the SecurityVulnerabilityAttribute attribute (this should not call the descructor for the AstAttribute object)
  551. astNode->removeAttribute("SecurityVulnerabilityAttribute");
  552. if (astNode->attributeExists("PrimarySecurityVulnerabilityForCloneAttribute") == false)
  553. {
  554. // AstAttribute* primaryVulnerabilityAttribute = new PrimarySecurityVulnerabilityForCloneAttribute(astNode);
  555. ROSE_ASSERT(securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer != NULL);
  556. PrimarySecurityVulnerabilityForCloneAttribute* primaryVulnerabilityAttribute =
  557. new PrimarySecurityVulnerabilityForCloneAttribute(astNode,rootOfClone,securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer);
  558. ROSE_ASSERT(primaryVulnerabilityAttribute != NULL);
  559. // astNode->addNewAttribute("SeededSecurityFlawCloneAttribute",primaryVulnerabilityAttribute);
  560. astNode->addNewAttribute("PrimarySecurityVulnerabilityForCloneAttribute",primaryVulnerabilityAttribute);
  561. ROSE_ASSERT(securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNode() != NULL);
  562. primaryVulnerabilityAttribute->set_primaryVulnerabilityInOriginalCode(securityVulnerabilityAttributeInOriginalCode->get_securityVulnerabilityNode());
  563. // if (astNode == primaryVulnerabilityNodeInClone)
  564. // addComment (astNode,std::string("*** NOTE Primary Node for clone: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttributeInClonedCode->vulnerabilityPointer->get_name() );
  565. }
  566. else
  567. {
  568. // We may have to deal with a single IR node having several PrimarySecurityVulnerabilityForCloneAttribute
  569. // associated with different security flaws. But for now I want to make this an error!
  570. printf ("Error: this IR nodes already has a PrimarySecurityVulnerabilityForCloneAttribute \n");
  571. ROSE_ASSERT(false);
  572. }
  573. }
  574. }
  575. }
  576. // void BufferOverFlowSecurityFlaw::CloneVulnerabilityTraversal::visit( SgNode* astNode )
  577. SecurityFlaw::CloneSeedLocation::InheritedAttribute
  578. SecurityFlaw::CloneSeedLocation::CloneSeedLocationTraversal::evaluateInheritedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute )
  579. {
  580. ROSE_ASSERT(astNode != NULL);
  581. // At this level were we are clonig to introduce different seeding methodologoes, we want to take actions
  582. // at the PrimarySecurityVulnerabilityForCloneAttribute objects previously left in the AST from previous
  583. // cloning and marking operations.
  584. AstAttribute* existingAttribute = astNode->getAttribute("PrimarySecurityVulnerabilityForCloneAttribute");
  585. PrimarySecurityVulnerabilityForCloneAttribute* securityVulnerabilityAttribute = dynamic_cast<PrimarySecurityVulnerabilityForCloneAttribute*>(existingAttribute);
  586. if (securityVulnerabilityAttribute != NULL)
  587. {
  588. // ROSE_ASSERT(securityVulnerabilityAttribute->get_securityVulnerabilityNode() == astNode);
  589. ROSE_ASSERT(associtedSeedSecurityFlaw != NULL);
  590. vector<SgNode*> grainularityAxis = associtedSeedSecurityFlaw->grainularityOfSeededCode(astNode);
  591. // Iterate from finest level of grainularity (e.g. SgExpression or SgStatement) to largest
  592. // level of grainularity (e.g. SgFunctionDeclaration or SgFile). This is most often a
  593. // container of size == 1.
  594. printf ("grainularityAxis.size() = %lu \n",grainularityAxis.size());
  595. #if 1
  596. // I think this is the better choice but I am not certain.
  597. vector<SgNode*>::reverse_iterator i = grainularityAxis.rbegin();
  598. while (i != grainularityAxis.rend())
  599. #else
  600. vector<SgNode*>::iterator i = grainularityAxis.begin();
  601. while (i != grainularityAxis.end())
  602. #endif
  603. {
  604. // Put code here to build AST copy and transform the copy!
  605. SgNode* subtree = *i;
  606. printf ("subtree = %p = %s \n",subtree,subtree->class_name().c_str());
  607. SgStatement* subTreeStatement = isSgStatement(subtree);
  608. ROSE_ASSERT(subTreeStatement != NULL);
  609. ROSE_ASSERT(securityVulnerabilityAttribute->vulnerabilityPointer != NULL);
  610. printf ("XXXXXXX In SecurityFlaw::codeCloneGeneration(): associatedSeedingTechniques.size() = %zu \n",securityVulnerabilityAttribute->vulnerabilityPointer->associatedSeedingTechniques.size());
  611. std::set<SeedSecurityFlaw*>::iterator j = securityVulnerabilityAttribute->vulnerabilityPointer->associatedSeedingTechniques.begin();
  612. while (j != securityVulnerabilityAttribute->vulnerabilityPointer->associatedSeedingTechniques.end())
  613. {
  614. // ******************************
  615. // Make a copy of the expression used to hold the array size in the array declaration.
  616. SgTreeCopy subTreeCopyHelp;
  617. SgStatement* nearestWholeStatementCopy = isSgStatement(subtree->copy(subTreeCopyHelp));
  618. ROSE_ASSERT(nearestWholeStatementCopy != NULL);
  619. printf ("Calling resetAttributesAfterASTCloning... \n");
  620. resetAttributesAfterASTCloning(nearestWholeStatementCopy);
  621. printf ("DONE: Calling resetAttributesAfterASTCloning... \n");
  622. // DQ (7/26/2008): This is likely the most significant change from where this code was copied from the SecurityFlaw::CloneVulnerability class
  623. // Mark the original subtree that is being copied as the original (so that we can optionally permit not seeding the original code).
  624. // Note that we may be visiting this IR node for a second time so it might already have an existing SecurityFlawOriginalSubtreeAttribute.
  625. // AstAttribute* originalSubtreeAttribute = new SeedMethodologyCloneAttribute(nearestWholeStatementCopy,subtree,associtedSeedSecurityFlaw);
  626. AstAttribute* originalSubtreeAttribute = new SeedMethodologyCloneAttribute(nearestWholeStatementCopy,subtree,*j);
  627. ROSE_ASSERT(originalSubtreeAttribute != NULL);
  628. if (nearestWholeStatementCopy->attributeExists("SeedMethodologyCloneAttribute") == false)
  629. {
  630. nearestWholeStatementCopy->addNewAttribute("SeedMethodologyCloneAttribute",originalSubtreeAttribute);
  631. // Also remove the copy that was make of the SecurityFlawOriginalSubtreeAttribute (of the grainularity of the cloning was the same).
  632. if (nearestWholeStatementCopy->attributeExists("SeededSecurityFlawCloneAttribute") == true)
  633. {
  634. nearestWholeStatementCopy->removeAttribute("SeededSecurityFlawCloneAttribute");
  635. }
  636. }
  637. #if 0
  638. // Mark the copy as a copy built only for seeding security flaws (record the primary flaw in the original AST).
  639. // AstAttribute* cloneSubtreeAttribute = new SeededSecurityFlawCloneAttribute(astNode,subtree);
  640. AstAttribute* cloneSubtreeAttribute = new SeededSecurityFlawCloneAttribute(nearestWholeStatementCopy,subtree);
  641. ROSE_ASSERT(cloneSubtreeAttribute != NULL);
  642. // nearestWholeStatementCopy->addNewAttribute("SeededSecurityFlawCloneAttribute",cloneSubtreeAttribute);
  643. if (nearestWholeStatementCopy->attributeExists("SeededSecurityFlawCloneAttribute") == false)
  644. {
  645. // If the subTreeStatement has been copied previously then it had a SecurityFlawOriginalSubtreeAttribute
  646. // attribute added and we want to remove it from the copy just make.
  647. if (nearestWholeStatementCopy->attributeExists("SecurityFlawOriginalSubtreeAttribute") == true)
  648. {
  649. nearestWholeStatementCopy->removeAttribute("SecurityFlawOriginalSubtreeAttribute");
  650. }
  651. nearestWholeStatementCopy->addNewAttribute("SeededSecurityFlawCloneAttribute",cloneSubtreeAttribute);
  652. // Link from SecurityVulnerabilityAttribute to the clone that was generated to support its seeding.
  653. // securityVulnerabilityAttribute->set_associatedClone(nearestWholeStatementCopy);
  654. // Mark the clone's primary vulnerability (i.e. the reason why we bothered to make the clone).
  655. SgNode* locationOfPrimaryVulnerabilityInClone = NULL;
  656. markPrimaryCloneVulnerability(locationOfPrimaryVulnerabilityInClone,astNode,nearestWholeStatementCopy);
  657. }
  658. #endif
  659. // Insert the new statement after the statement with the security vulnerability (at the defined level of grainularity)
  660. if (isSgFunctionDeclaration(nearestWholeStatementCopy) != NULL)
  661. {
  662. // Insert functions at the base of the current scope
  663. subTreeStatement->get_scope()->insertStatementInScope(nearestWholeStatementCopy,false);
  664. }
  665. else
  666. {
  667. // Insert statements after the current statement
  668. subTreeStatement->get_scope()->insert_statement(subTreeStatement,nearestWholeStatementCopy,false);
  669. }
  670. // Note that SgFunctionDeclaration IR nodes have the additional detail of requiring symbols to be added.
  671. SgFunctionDeclaration* functionDeclaration = isSgFunctionDeclaration(nearestWholeStatementCopy);
  672. if (functionDeclaration != NULL)
  673. {
  674. SgName functionName = functionDeclaration->get_name();
  675. functionName += string("_SecurityFlawSeeded_function_") + StringUtility::numberToString(uniqueValueSeeding());
  676. functionDeclaration->set_name(functionName);
  677. printf ("functionName = %s \n",functionName.str());
  678. ROSE_ASSERT(functionDeclaration->get_scope() != NULL);
  679. SgFunctionSymbol* functionSymbol = new SgFunctionSymbol(functionDeclaration);
  680. SgName mangledName = functionDeclaration->get_mangled_name();
  681. functionDeclaration->get_scope()->insert_symbol(functionName,functionSymbol);
  682. // functionDeclaration->get_scope()->insert_symbol(mangledName,functionSymbol);
  683. functionSymbol->set_parent(functionDeclaration->get_scope()->get_symbol_table());
  684. // addComment (nearestWholeStatementCopy,"*** NOTE Function Containing Seeded Security Flaw: BufferOverFlowSecurityFlaw ");
  685. // error checking
  686. SgSymbol* local_symbol = functionDeclaration->get_symbol_from_symbol_table();
  687. ROSE_ASSERT(local_symbol != NULL);
  688. }
  689. // ******************************
  690. j++;
  691. }
  692. i++;
  693. }
  694. }
  695. return inheritedAttribute;
  696. }
  697. // **********************************************************************
  698. // SecurityFlaw::MarkClonesTraversal
  699. // **********************************************************************
  700. SecurityFlaw::MarkClones::MarkClonesTraversal::MarkClonesTraversal( SecurityFlaw::SeedSecurityFlaw* Xptr )
  701. {
  702. ROSE_ASSERT(Xptr != NULL);
  703. associtedSeedSecurityFlaw = Xptr;
  704. }
  705. SecurityFlaw::MarkClones::MarkClonesTraversal::~MarkClonesTraversal()
  706. {
  707. associtedSeedSecurityFlaw = NULL;
  708. }
  709. void
  710. SecurityFlaw::MarkClones::markVulnerabilitiesInClones( SgProject* project, SecurityFlaw::SeedSecurityFlaw* flaw )
  711. {
  712. // Build an AST traversal object
  713. MarkClonesTraversal treeTraversal(flaw);
  714. InheritedAttribute inheritedAttribute;
  715. // This traverses only the input source file (to traverse all header file
  716. // and the source file call "traverse" instead of "traverseInputFiles").
  717. // treeTraversal.traverseInputFiles (project,preorder);
  718. treeTraversal.traverseInputFiles (project,inheritedAttribute);
  719. }
  720. // void BufferOverFlowSecurityFlaw::CloneVulnerabilityTraversal::visit( SgNode* astNode )
  721. SecurityFlaw::MarkClones::InheritedAttribute
  722. SecurityFlaw::MarkClones::MarkClonesTraversal::evaluateInheritedAttribute ( SgNode* astNode, InheritedAttribute inheritedAttribute )
  723. {
  724. // This function calls the seeding traversal on the AST formed by:
  725. // 1) Finding the SecurityVulnerabilityAttribute marker
  726. // 2) Backing up the AST to a specific level of grainularity
  727. // 3) Then removing any clones from subtrees of the original code.
  728. ROSE_ASSERT(astNode != NULL);
  729. #if 0
  730. printf ("astNode = %p = %s \n",astNode,astNode->class_name().c_str());
  731. printf ("inheritedAttribute.inOriginalCode = %s \n",inheritedAttribute.inOriginalCode ? "true" : "false");
  732. printf ("inheritedAttribute.inClonedCode = %s \n",inheritedAttribute.inClonedCode ? "true" : "false");
  733. #endif
  734. if (astNode->attributeExists("SecurityFlawOriginalSubtreeAttribute") == true)
  735. {
  736. printf ("Found a SecurityFlawOriginalSubtreeAttribute \n");
  737. inheritedAttribute.inOriginalCode = true;
  738. ROSE_ASSERT(inheritedAttribute.inClonedCode == false);
  739. }
  740. if (astNode->attributeExists("SeededSecurityFlawCloneAttribute") == true)
  741. {
  742. printf ("Found a SeededSecurityFlawCloneAttribute \n");
  743. inheritedAttribute.inClonedCode = true;
  744. ROSE_ASSERT(inheritedAttribute.inOriginalCode == false);
  745. }
  746. AstAttribute* existingAttribute = astNode->getAttribute("SecurityVulnerabilityAttribute");
  747. SecurityVulnerabilityAttribute* securityVulnerabilityAttribute = dynamic_cast<SecurityVulnerabilityAttribute*>(existingAttribute);
  748. // Use the value to code specific types of vulnerabilities
  749. // if (securityVulnerabilityAttribute != NULL && securityVulnerabilityAttribute->get_value() == 5)
  750. if (securityVulnerabilityAttribute != NULL)
  751. {
  752. if (inheritedAttribute.inOriginalCode == true)
  753. {
  754. ROSE_ASSERT(securityVulnerabilityAttribute->get_securityVulnerabilityNode() == astNode);
  755. // addComment (astNode,string("*** NOTE Original Security Flaw Vulnerability: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttribute->vulnerabilityPointer->get_name() );
  756. }
  757. else
  758. {
  759. // DQ (7/27/2008): Now that we support deep copy on attributes this is false
  760. // ROSE_ASSERT(securityVulnerabilityAttribute->get_securityVulnerabilityNode() != astNode);
  761. if (inheritedAttribute.inClonedCode == true)
  762. {
  763. // addComment (astNode,std::string("*** NOTE Cloned Security Flaw Vulnerability: BufferOverFlowSecurityFlaw ") + securityVulnerabilityAttribute->vulnerabilityPointer->get_name() );
  764. #if 0
  765. astNode->removeAttribute("SecurityVulnerabilityAttribute");
  766. // Build a new SecurityVulnerabilityAttribute attribute to replace the one that was shared
  767. // with the origial AST (copy of AST attributes is not deep).
  768. AstAttribute* newAttribute = new SecurityVulnerabilityAttribute(astNode,securityVulnerabilityAttribute->vulnerabilityPointer);
  769. ROSE_ASSERT(newAttribute != NULL);
  770. // We need to name the attributes, but all the VulnerabilityAttributes can all have the same name.
  771. // It is easier to distinquish them by value (stored internally in the attribute). The use of
  772. // names for attributes permits other forms of analysis to use attributes in the same AST and
  773. // for all the forms of analysis to co-exist (so long as the select unique names).
  774. astNode->addNewAttribute("SecurityVulnerabilityAttribute",newAttribute);
  775. #else
  776. // DQ (7/27/2008): Now that we have fixed this bug in ROSE we can do less work!
  777. printf ("Because we now have deep copies on AST attributes, this code (building new SecurityVulnerabilityAttribute objects) is not required. \n");
  778. #endif
  779. }
  780. else
  781. {
  782. printf ("Outside of of both original code and cloned code! astNode = %p = %s \n",astNode,astNode->class_name().c_str());
  783. }
  784. }
  785. }
  786. return inheritedAttribute;
  787. }
  788. // **********************************************************************
  789. // SecurityFlaw::CommentClones
  790. // **********************************************************************
  791. void
  792. SecurityFlaw::CommentClones::commentClones( SgProject* project )
  793. {
  794. // Build an AST traversal object
  795. CommentClonesTraversal treeTraversal;
  796. // This traverses only the input sou…

Large files files are truncated, but you can click here to view the full file