kerneltest/e32test/secure/t_sdrivers.cpp
changeset 9 96e5fb8b040d
child 43 c1f20ce4abcf
equal deleted inserted replaced
-1:000000000000 9:96e5fb8b040d
       
     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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\secure\t_sdrivers.cpp
       
    15 // Overview:
       
    16 // Test the security aspects of device drivers.
       
    17 // API Information:
       
    18 // N/A
       
    19 // Details:
       
    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:
       
    23 // All.
       
    24 // Assumptions/Requirement/Pre-requisites:
       
    25 // Failures and causes:
       
    26 // Base Port information:
       
    27 // 
       
    28 //
       
    29 
       
    30 #define __INCLUDE_CAPABILITY_NAMES__
       
    31 
       
    32 #define __E32TEST_EXTENSION__
       
    33 #include <e32test.h>
       
    34 #include <e32svr.h>
       
    35 // SYM_BRANCH: Delete old sound driver
       
    36 // #include <mdasound.h>
       
    37 #include <d32comm.h>
       
    38 #include <d32usbc.h>
       
    39 #include <d32ethernet.h>
       
    40 
       
    41 LOCAL_D RTest test(_L("T_SDRIVERS"));
       
    42 
       
    43 TCapabilitySet Capabilities;
       
    44 
       
    45 LOCAL_C TBool CheckLoaded(TInt aResult)
       
    46 	{
       
    47 	switch(aResult)
       
    48 		{
       
    49 	case KErrAlreadyExists:
       
    50 		RDebug::Print(_L("  Already Exists"));
       
    51 		return ETrue;
       
    52 
       
    53 	case KErrNone:
       
    54 		RDebug::Print(_L("  No Error"));
       
    55 		return ETrue;
       
    56 
       
    57 	case KErrNotFound:
       
    58 		RDebug::Print(_L("  Not found"));
       
    59 		return EFalse;
       
    60 
       
    61 	default:
       
    62 		test(EFalse);
       
    63 		return EFalse;
       
    64 		}
       
    65 	}
       
    66 
       
    67 
       
    68 class TDriverCheck;
       
    69 typedef void (*TDriverCheckTestFunction)(TDriverCheck&);
       
    70 
       
    71 class TDriverCheck
       
    72 	{
       
    73 public:
       
    74 	TBool Check(TInt aResult);
       
    75 	void ShowResult();
       
    76 public:
       
    77 	TDriverCheckTestFunction iTestFunction;
       
    78 	const char* iDeviceName;
       
    79 	TCapability iCapability;
       
    80 	TBool iTested;
       
    81 	TBool iPolicingVerified;
       
    82 	};
       
    83 
       
    84 TBool TDriverCheck::Check(TInt aResult)
       
    85 	{
       
    86 	switch(aResult)
       
    87 		{
       
    88 	case KErrNotSupported:
       
    89 		RDebug::Print(_L("  Not Supported"));
       
    90 		return ETrue;
       
    91 	case KErrInUse:
       
    92 		RDebug::Print(_L("  In Use"));
       
    93 		return ETrue;
       
    94 	case KErrAccessDenied:
       
    95 		RDebug::Print(_L("  Access Denied (In Use?)"));
       
    96 		return ETrue;
       
    97 	case KErrNone:
       
    98 		RDebug::Print(_L("  No Error"));
       
    99 		break;
       
   100 	case KErrPermissionDenied:
       
   101 		RDebug::Print(_L("  Permission Denied"));
       
   102 		break;
       
   103 	default:
       
   104 		RDebug::Print(_L("  Error %d"),aResult);
       
   105 		return ETrue;
       
   106 		}
       
   107 
       
   108 	if(Capabilities.HasCapability(iCapability))
       
   109 		{
       
   110 		if(aResult==KErrNone)
       
   111 			iTested = 1;
       
   112 		return aResult==KErrNone;
       
   113 		}
       
   114 	else if(PlatSec::IsCapabilityEnforced(iCapability))
       
   115 		{
       
   116 		if(aResult==KErrPermissionDenied)
       
   117 			iPolicingVerified = 1;
       
   118 		return aResult==KErrPermissionDenied;
       
   119 		}
       
   120 	else
       
   121 		{
       
   122 		return aResult==KErrNone;
       
   123 		}
       
   124 	}
       
   125 
       
   126 void TDriverCheck::ShowResult()
       
   127 	{
       
   128 	TBuf8<32> nameBuf((const TUint8*)iDeviceName);
       
   129 	TPtr name(nameBuf.Expand());
       
   130 	if(iTested)
       
   131 		{
       
   132 		if(iPolicingVerified)
       
   133 			test.Printf(_L("*  %S - Verified security checking\n"),&name);
       
   134 		else
       
   135 			test.Printf(_L("*  %S - Did NOT verify security checking (Capabilties may be disabled)\n"),&name);
       
   136 		}
       
   137 	else
       
   138 		test.Printf(_L("*  %S - Not tested (Driver may be missing or in use)\n"),&name);
       
   139 	}
       
   140 
       
   141 void TestELOCD(TDriverCheck& aCheck)
       
   142 	{
       
   143 	test.Next(_L("ELOCD"));
       
   144 
       
   145 	test.Start(_L("Trying RLocalDrive with all local drives"));
       
   146 	TInt i;
       
   147 	TInt r;
       
   148 	for(i=0; i<KMaxLocalDrives; i++)
       
   149 		{
       
   150 		RLocalDrive localDrive;
       
   151 		TInt changedFlag = 0;
       
   152 		r = localDrive.Connect(i,changedFlag);
       
   153 		test(aCheck.Check(r));
       
   154 		localDrive.Close();
       
   155 		}
       
   156 	test.End();
       
   157 	}
       
   158 
       
   159 #if defined (__WINS__)
       
   160 #define COMM_PDD_NAME _L("ECDRV.PDD")
       
   161 const TInt KMaxCommPdds=0;
       
   162 #else
       
   163 #define COMM_PDD_NAME _L("EUART")
       
   164 const TInt KMaxCommPdds=10;
       
   165 #endif
       
   166 
       
   167 void TestECOMM(TDriverCheck& aCheck)
       
   168 	{
       
   169 	test.Next(_L("ECOMM"));
       
   170 
       
   171 	test.Start(_L("Load PDDs"));
       
   172 	TInt i;
       
   173 	TInt r;
       
   174     TBuf<10> pddName=COMM_PDD_NAME;
       
   175 	for (i=-1; i<KMaxCommPdds; ++i)
       
   176 		{
       
   177 		if (i==0)
       
   178 			pddName.Append(TChar('0'));
       
   179 		else if (i>0)
       
   180 			pddName[pddName.Length()-1] = (TText)('0'+i);
       
   181 		r = User::LoadPhysicalDevice(pddName);
       
   182 		CheckLoaded(r);
       
   183 		}
       
   184 	test.Next(_L("Load LDD"));
       
   185 	r = User::LoadLogicalDevice(_L("ECOMM.LDD"));
       
   186 	if(!CheckLoaded(r))
       
   187 		goto done;
       
   188 
       
   189 	test.Next(_L("Open Channels"));
       
   190 	for(i=0; i<10; i++)
       
   191 		{
       
   192 		RBusDevComm commDevice;
       
   193 		r = commDevice.Open(i);
       
   194 		test(aCheck.Check(r));
       
   195 		commDevice.Close();
       
   196 		}
       
   197 done:
       
   198 	test.End();
       
   199 	}
       
   200 
       
   201 void TestEUSBC(TDriverCheck& aCheck)
       
   202 	{
       
   203 	test.Next(_L("EUSBC"));
       
   204 
       
   205 	test.Start(_L("Load LDD"));
       
   206 	TInt r = User::LoadLogicalDevice(_L("EUSBC.LDD"));
       
   207 	if(!CheckLoaded(r))
       
   208 		goto done;
       
   209 	test.Next(_L("Open Channel"));
       
   210 	{
       
   211 	RDevUsbcClient usbDevice;
       
   212 	r = usbDevice.Open(0);
       
   213 	test(aCheck.Check(r));
       
   214 	usbDevice.Close();
       
   215 	}
       
   216 done:
       
   217 	test.End();
       
   218 	}
       
   219 
       
   220 void TestENET(TDriverCheck& aCheck)
       
   221 	{
       
   222 	test.Next(_L("ENET"));
       
   223 
       
   224 	test.Start(_L("Load PDD"));
       
   225 	TInt r = User::LoadPhysicalDevice(_L("ETHERNET.PDD"));
       
   226 	if(!CheckLoaded(r))
       
   227 		goto done;
       
   228 	test.Start(_L("Load LDD"));
       
   229 	r = User::LoadLogicalDevice(_L("ENET.LDD"));
       
   230 	if(!CheckLoaded(r))
       
   231 		goto done;
       
   232 	test.Next(_L("Open Channel"));
       
   233 	{
       
   234 	RBusDevEthernet ethernetDevice;
       
   235 	r = ethernetDevice.Open(0);
       
   236 	test(aCheck.Check(r));
       
   237 	ethernetDevice.Close();
       
   238 	}
       
   239 done:
       
   240 	test.End();
       
   241 	}
       
   242 
       
   243 // SYM_BRANCH: Delete old sound driver
       
   244 #if 0
       
   245 void TestESOUND(TDriverCheck& aCheck)
       
   246 	{
       
   247 	test.Next(_L("ESOUND"));
       
   248 
       
   249 	test.Start(_L("Load PDD"));
       
   250 	TInt r = User::LoadPhysicalDevice(_L("ESDRV.PDD"));
       
   251 	if(!CheckLoaded(r))
       
   252 		goto done;
       
   253 	test.Next(_L("Load LDD"));
       
   254 	r = User::LoadLogicalDevice(_L("ESOUND.LDD"));
       
   255 	if(!CheckLoaded(r))
       
   256 		goto done;
       
   257 	test.Next(_L("Open Channel"));
       
   258 	{
       
   259 	RMdaDevSound soundDevice;
       
   260 	r = soundDevice.Open();
       
   261 	test(aCheck.Check(r));
       
   262 	soundDevice.Close();
       
   263 	}
       
   264 done:
       
   265 	test.End();
       
   266 	}
       
   267 #endif
       
   268 
       
   269 TDriverCheck DriverList[] =
       
   270 	{
       
   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},
       
   277 		{0}
       
   278 	};
       
   279 
       
   280 LOCAL_C TInt DoTests()
       
   281 	{
       
   282 	TInt result=0;
       
   283 	test.Start(_L("Testing all LDDs..."));
       
   284 	TInt i=0;
       
   285 	while(DriverList[i].iTestFunction)
       
   286 		{
       
   287 		(*DriverList[i].iTestFunction)(DriverList[i]);
       
   288 		result |= DriverList[i].iTested<<(i*2);
       
   289 		result |= DriverList[i].iPolicingVerified<<(i*2+1);
       
   290 		++i;
       
   291 		}
       
   292 	test.End();
       
   293 	return result^0x55555555;
       
   294 	}
       
   295 
       
   296 
       
   297 enum TTestProcessFunctions
       
   298 	{
       
   299 	ETestProcessDoTests,
       
   300 	};
       
   301 
       
   302 
       
   303 #include "d_sldd.h"
       
   304 #include "u32std.h"
       
   305 
       
   306 RDevice TestDevice;
       
   307 
       
   308 TInt TestGetCapsThread(TAny* aDes)
       
   309 	{
       
   310 	RThread::Rendezvous(KErrNone);
       
   311 	TestDevice.GetCaps(*(TDes8*)aDes);
       
   312 	return KErrNone;
       
   313 	}
       
   314 
       
   315 void TestGetCaps()
       
   316 	{
       
   317 	TUint memModelAttributes = UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL);
       
   318 	if((memModelAttributes&EMemModelAttrKernProt)==false)
       
   319 		return; // no kernel protection to test
       
   320 
       
   321 	// open test device...
       
   322 	test.Start(_L("Open test driver"));
       
   323 	RLddTest ldd;
       
   324 	_LIT(KTestDeviceName,"D_SLDD");
       
   325 	TInt r = User::LoadLogicalDevice(KTestDeviceName);
       
   326 	test(r == KErrNone || r == KErrAlreadyExists);
       
   327 	r = TestDevice.Open(KTestDeviceName);
       
   328 	test_KErrNone(r);
       
   329 	r = ldd.OpenLocal();
       
   330 	test_KErrNone(r);
       
   331 
       
   332 	// get address of some kernel data...
       
   333 	TUint32* kernelPtr;
       
   334 	TUint32 kernelData;
       
   335 	ldd.KernelTestData(kernelPtr,kernelData);
       
   336 
       
   337 	// check device GetCaps works...
       
   338 	test.Next(_L("Check GetCaps"));
       
   339 	_LIT8(KDummyTestData,"Dummy Test Data");
       
   340 	TBuf8<256> caps;
       
   341 	caps.Copy(KDummyTestData);
       
   342 	test(caps.Compare(KDummyTestData)==0);
       
   343 	TestDevice.GetCaps(caps);
       
   344 	test(caps.Compare(KDummyTestData)!=0);
       
   345 
       
   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));
       
   349 	RThread thread;
       
   350 	r = thread.Create(_L("TestGetCapsThread"),TestGetCapsThread,KDefaultStackSize,0x2000,0x2000,(TAny*)&badCaps);
       
   351 	test_KErrNone(r);
       
   352 	TRequestStatus ls;
       
   353 	thread.Logon(ls);
       
   354 	TRequestStatus rs;
       
   355 	thread.Rendezvous(rs);
       
   356 	thread.Resume();
       
   357 	User::WaitForRequest(rs);
       
   358 	test_KErrNone(rs.Int());
       
   359 	User::WaitForRequest(ls);
       
   360 	test_Equal(EExitPanic,thread.ExitType());
       
   361 	thread.Close();
       
   362 
       
   363 	// check kernel data is unchanged...
       
   364 	TUint32 kernelData2;
       
   365 	ldd.KernelTestData(kernelPtr,kernelData2);
       
   366 	test_Equal(kernelData,kernelData2);
       
   367 
       
   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);
       
   371 	test_KErrNone(r);
       
   372 	thread.Logon(ls);
       
   373 	thread.Rendezvous(rs);
       
   374 	thread.Resume();
       
   375 	User::WaitForRequest(rs);
       
   376 	test_KErrNone(rs.Int());
       
   377 	User::WaitForRequest(ls);
       
   378 	test_Equal(EExitPanic,thread.ExitType());
       
   379 	thread.Close();
       
   380 
       
   381 	// check kernel data is unchanged...
       
   382 	ldd.KernelTestData(kernelPtr,kernelData2);
       
   383 	test_Equal(kernelData,kernelData2);
       
   384 
       
   385 	// cleanup...
       
   386 	ldd.Close();
       
   387 	TestDevice.Close();
       
   388 
       
   389 	test.End();
       
   390 	}
       
   391 
       
   392 
       
   393 #include "testprocess.h"
       
   394 
       
   395 
       
   396 GLDEF_C TInt E32Main()
       
   397     {
       
   398 	Capabilities = TSecurityInfo(RProcess()).iCaps;
       
   399 
       
   400 	test.Title();
       
   401 
       
   402 	if(User::CommandLineLength())
       
   403 		{
       
   404 		TBuf<128> message;
       
   405 		__ASSERT_COMPILE(ECapability_Limit<64);
       
   406 		message.AppendFormat(_L("Tests with capabilities %08x%08x"),((TUint32*)&Capabilities)[1],((TUint32*)&Capabilities)[0]);
       
   407 		test.Start(message);
       
   408 		TInt result = DoTests();
       
   409 		// Don't test.End() so we don't get lots of 'Success's in logs
       
   410 		return(result);
       
   411 		}
       
   412 
       
   413 	test.Start(_L("Start"));
       
   414 
       
   415 	test.Next(_L("Check driver GetCaps() vulnerability"));
       
   416 	TestGetCaps();
       
   417 
       
   418 	TInt i;
       
   419 	TInt c;
       
   420 	for(c=0; c<ECapability_Limit; c++)
       
   421 		{
       
   422 		RTestProcess p;
       
   423 		TRequestStatus s;
       
   424 		TBuf<128> message;
       
   425 		TCapabilitySet caps;
       
   426 		caps.SetAllSupported();
       
   427 		if(!caps.HasCapability((TCapability)c))
       
   428 			continue;
       
   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);
       
   434 		test.Next(message);
       
   435 		p.Create(*(TUint32*)&caps,ETestProcessDoTests);
       
   436 		p.Logon(s);
       
   437 		p.Resume();
       
   438 		User::WaitForRequest(s);
       
   439 		test(p.ExitType()==EExitKill);
       
   440 		TInt result=s.Int()^0x55555555;
       
   441 		i=0;
       
   442 		while(DriverList[i].iTestFunction)
       
   443 			{
       
   444 			if(result & (1<<(i*2)))
       
   445 				DriverList[i].iTested = ETrue;
       
   446 			if(result & (1<<(i*2+1)))
       
   447 				DriverList[i].iPolicingVerified = ETrue;
       
   448 			++i;
       
   449 			}
       
   450 		test((result>>(i*2))==0);
       
   451 		CLOSE_AND_WAIT(p);
       
   452 		}
       
   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);
       
   458 	i=0;
       
   459 	while(DriverList[i].iTestFunction)
       
   460 		{
       
   461 		DriverList[i].ShowResult();
       
   462 		++i;
       
   463 		}
       
   464 	test.Printf(KSeperatorText);
       
   465 
       
   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);
       
   470 	RTimer timer;
       
   471 	test(timer.CreateLocal()==KErrNone);
       
   472 	TRequestStatus timerStat;
       
   473 	timer.After(timerStat,20*1000000);
       
   474 	User::WaitForRequest(timerStat,keyStat);
       
   475 	TInt key = 0;
       
   476 	if(keyStat!=KRequestPending)
       
   477 		key = test.Console()->KeyCode();
       
   478 	timer.Cancel();
       
   479 	test.Console()->ReadCancel();
       
   480 	User::WaitForAnyRequest();
       
   481 
       
   482 	test.End();
       
   483 	return(0);
       
   484     }
       
   485