First public contribution.
1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\secure\t_sdrivers.cpp
16 // Test the security aspects of device drivers.
20 // - For a variety of capability sets, test loading and opening various
21 // devices and check that the results are as expected.
22 // Platforms/Drives/Compatibility:
24 // Assumptions/Requirement/Pre-requisites:
25 // Failures and causes:
26 // Base Port information:
30 #define __INCLUDE_CAPABILITY_NAMES__
32 #define __E32TEST_EXTENSION__
35 // SYM_BRANCH: Delete old sound driver
36 // #include <mdasound.h>
39 #include <d32ethernet.h>
41 LOCAL_D RTest test(_L("T_SDRIVERS"));
43 TCapabilitySet Capabilities;
45 LOCAL_C TBool CheckLoaded(TInt aResult)
49 case KErrAlreadyExists:
50 RDebug::Print(_L(" Already Exists"));
54 RDebug::Print(_L(" No Error"));
58 RDebug::Print(_L(" Not found"));
69 typedef void (*TDriverCheckTestFunction)(TDriverCheck&);
74 TBool Check(TInt aResult);
77 TDriverCheckTestFunction iTestFunction;
78 const char* iDeviceName;
79 TCapability iCapability;
81 TBool iPolicingVerified;
84 TBool TDriverCheck::Check(TInt aResult)
88 case KErrNotSupported:
89 RDebug::Print(_L(" Not Supported"));
92 RDebug::Print(_L(" In Use"));
94 case KErrAccessDenied:
95 RDebug::Print(_L(" Access Denied (In Use?)"));
98 RDebug::Print(_L(" No Error"));
100 case KErrPermissionDenied:
101 RDebug::Print(_L(" Permission Denied"));
104 RDebug::Print(_L(" Error %d"),aResult);
108 if(Capabilities.HasCapability(iCapability))
110 if(aResult==KErrNone)
112 return aResult==KErrNone;
114 else if(PlatSec::IsCapabilityEnforced(iCapability))
116 if(aResult==KErrPermissionDenied)
117 iPolicingVerified = 1;
118 return aResult==KErrPermissionDenied;
122 return aResult==KErrNone;
126 void TDriverCheck::ShowResult()
128 TBuf8<32> nameBuf((const TUint8*)iDeviceName);
129 TPtr name(nameBuf.Expand());
132 if(iPolicingVerified)
133 test.Printf(_L("* %S - Verified security checking\n"),&name);
135 test.Printf(_L("* %S - Did NOT verify security checking (Capabilties may be disabled)\n"),&name);
138 test.Printf(_L("* %S - Not tested (Driver may be missing or in use)\n"),&name);
141 void TestELOCD(TDriverCheck& aCheck)
143 test.Next(_L("ELOCD"));
145 test.Start(_L("Trying RLocalDrive with all local drives"));
148 for(i=0; i<KMaxLocalDrives; i++)
150 RLocalDrive localDrive;
151 TInt changedFlag = 0;
152 r = localDrive.Connect(i,changedFlag);
153 test(aCheck.Check(r));
159 #if defined (__WINS__)
160 #define COMM_PDD_NAME _L("ECDRV.PDD")
161 const TInt KMaxCommPdds=0;
163 #define COMM_PDD_NAME _L("EUART")
164 const TInt KMaxCommPdds=10;
167 void TestECOMM(TDriverCheck& aCheck)
169 test.Next(_L("ECOMM"));
171 test.Start(_L("Load PDDs"));
174 TBuf<10> pddName=COMM_PDD_NAME;
175 for (i=-1; i<KMaxCommPdds; ++i)
178 pddName.Append(TChar('0'));
180 pddName[pddName.Length()-1] = (TText)('0'+i);
181 r = User::LoadPhysicalDevice(pddName);
184 test.Next(_L("Load LDD"));
185 r = User::LoadLogicalDevice(_L("ECOMM.LDD"));
189 test.Next(_L("Open Channels"));
192 RBusDevComm commDevice;
193 r = commDevice.Open(i);
194 test(aCheck.Check(r));
201 void TestEUSBC(TDriverCheck& aCheck)
203 test.Next(_L("EUSBC"));
205 test.Start(_L("Load LDD"));
206 TInt r = User::LoadLogicalDevice(_L("EUSBC.LDD"));
209 test.Next(_L("Open Channel"));
211 RDevUsbcClient usbDevice;
212 r = usbDevice.Open(0);
213 test(aCheck.Check(r));
220 void TestENET(TDriverCheck& aCheck)
222 test.Next(_L("ENET"));
224 test.Start(_L("Load PDD"));
225 TInt r = User::LoadPhysicalDevice(_L("ETHERNET.PDD"));
228 test.Start(_L("Load LDD"));
229 r = User::LoadLogicalDevice(_L("ENET.LDD"));
232 test.Next(_L("Open Channel"));
234 RBusDevEthernet ethernetDevice;
235 r = ethernetDevice.Open(0);
236 test(aCheck.Check(r));
237 ethernetDevice.Close();
243 // SYM_BRANCH: Delete old sound driver
245 void TestESOUND(TDriverCheck& aCheck)
247 test.Next(_L("ESOUND"));
249 test.Start(_L("Load PDD"));
250 TInt r = User::LoadPhysicalDevice(_L("ESDRV.PDD"));
253 test.Next(_L("Load LDD"));
254 r = User::LoadLogicalDevice(_L("ESOUND.LDD"));
257 test.Next(_L("Open Channel"));
259 RMdaDevSound soundDevice;
260 r = soundDevice.Open();
261 test(aCheck.Check(r));
269 TDriverCheck DriverList[] =
271 {TestELOCD,"ELOCD",ECapabilityTCB},
272 {TestECOMM,"ECOMM",ECapabilityCommDD},
273 {TestEUSBC,"EUSBC",ECapabilityCommDD},
274 {TestENET,"ENET",ECapabilityCommDD},
275 // SYM_BRANCH: Delete old sound driver
276 // {TestESOUND,"ESOUND",ECapabilityMultimediaDD},
280 LOCAL_C TInt DoTests()
283 test.Start(_L("Testing all LDDs..."));
285 while(DriverList[i].iTestFunction)
287 (*DriverList[i].iTestFunction)(DriverList[i]);
288 result |= DriverList[i].iTested<<(i*2);
289 result |= DriverList[i].iPolicingVerified<<(i*2+1);
293 return result^0x55555555;
297 enum TTestProcessFunctions
308 TInt TestGetCapsThread(TAny* aDes)
310 RThread::Rendezvous(KErrNone);
311 TestDevice.GetCaps(*(TDes8*)aDes);
317 TUint memModelAttributes = UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL);
318 if((memModelAttributes&EMemModelAttrKernProt)==false)
319 return; // no kernel protection to test
321 // open test device...
322 test.Start(_L("Open test driver"));
324 _LIT(KTestDeviceName,"D_SLDD");
325 TInt r = User::LoadLogicalDevice(KTestDeviceName);
326 test(r == KErrNone || r == KErrAlreadyExists);
327 r = TestDevice.Open(KTestDeviceName);
332 // get address of some kernel data...
335 ldd.KernelTestData(kernelPtr,kernelData);
337 // check device GetCaps works...
338 test.Next(_L("Check GetCaps"));
339 _LIT8(KDummyTestData,"Dummy Test Data");
341 caps.Copy(KDummyTestData);
342 test(caps.Compare(KDummyTestData)==0);
343 TestDevice.GetCaps(caps);
344 test(caps.Compare(KDummyTestData)!=0);
346 // get another thread to try and call device GetCaps to write to kernel data...
347 test.Next(_L("Check GetCaps with bad descriptor"));
348 TPtr8 badCaps((TUint8*)kernelPtr,sizeof(TUint32));
350 r = thread.Create(_L("TestGetCapsThread"),TestGetCapsThread,KDefaultStackSize,0x2000,0x2000,(TAny*)&badCaps);
355 thread.Rendezvous(rs);
357 User::WaitForRequest(rs);
358 test_KErrNone(rs.Int());
359 User::WaitForRequest(ls);
360 test_Equal(EExitPanic,thread.ExitType());
363 // check kernel data is unchanged...
365 ldd.KernelTestData(kernelPtr,kernelData2);
366 test_Equal(kernelData,kernelData2);
368 // get another thread to try and call device GetCaps with descriptor in kernel memory...
369 test.Next(_L("Check GetCaps with bad descriptor 2"));
370 r = thread.Create(_L("TestGetCapsThread"),TestGetCapsThread,KDefaultStackSize,0x2000,0x2000,(TAny*)kernelPtr);
373 thread.Rendezvous(rs);
375 User::WaitForRequest(rs);
376 test_KErrNone(rs.Int());
377 User::WaitForRequest(ls);
378 test_Equal(EExitPanic,thread.ExitType());
381 // check kernel data is unchanged...
382 ldd.KernelTestData(kernelPtr,kernelData2);
383 test_Equal(kernelData,kernelData2);
393 #include "testprocess.h"
396 GLDEF_C TInt E32Main()
398 Capabilities = TSecurityInfo(RProcess()).iCaps;
402 if(User::CommandLineLength())
405 __ASSERT_COMPILE(ECapability_Limit<64);
406 message.AppendFormat(_L("Tests with capabilities %08x%08x"),((TUint32*)&Capabilities)[1],((TUint32*)&Capabilities)[0]);
408 TInt result = DoTests();
409 // Don't test.End() so we don't get lots of 'Success's in logs
413 test.Start(_L("Start"));
415 test.Next(_L("Check driver GetCaps() vulnerability"));
420 for(c=0; c<ECapability_Limit; c++)
426 caps.SetAllSupported();
427 if(!caps.HasCapability((TCapability)c))
429 caps.RemoveCapability((TCapability)c);
430 TBuf8<128> capNameBuf;
431 capNameBuf.Copy((const TUint8*)CapabilityNames[c]);
432 TPtr capName(capNameBuf.Expand());
433 message.AppendFormat(_L("Tests with all capabilities except %S"),&capName);
435 p.Create(*(TUint32*)&caps,ETestProcessDoTests);
438 User::WaitForRequest(s);
439 test(p.ExitType()==EExitKill);
440 TInt result=s.Int()^0x55555555;
442 while(DriverList[i].iTestFunction)
444 if(result & (1<<(i*2)))
445 DriverList[i].iTested = ETrue;
446 if(result & (1<<(i*2+1)))
447 DriverList[i].iPolicingVerified = ETrue;
450 test((result>>(i*2))==0);
453 // Show results requiring manual inspection
454 _LIT(KSeperatorText,"----------------------------------------------------------------------------\n");
455 test.Printf(_L("\n"));
456 test.Printf(_L("RESULTS\n"));
457 test.Printf(KSeperatorText);
459 while(DriverList[i].iTestFunction)
461 DriverList[i].ShowResult();
464 test.Printf(KSeperatorText);
466 // Wait for a while, or for a key press
467 test.Printf(_L("Waiting a short while for key press...\n"));
468 TRequestStatus keyStat;
469 test.Console()->Read(keyStat);
471 test(timer.CreateLocal()==KErrNone);
472 TRequestStatus timerStat;
473 timer.After(timerStat,20*1000000);
474 User::WaitForRequest(timerStat,keyStat);
476 if(keyStat!=KRequestPending)
477 key = test.Console()->KeyCode();
479 test.Console()->ReadCancel();
480 User::WaitForAnyRequest();