os/kernelhwsrv/kerneltest/e32test/secure/t_sdrivers.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/secure/t_sdrivers.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,485 @@
     1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\secure\t_sdrivers.cpp
    1.18 +// Overview:
    1.19 +// Test the security aspects of device drivers.
    1.20 +// API Information:
    1.21 +// N/A
    1.22 +// Details:
    1.23 +// - For a variety of capability sets, test loading and opening various 
    1.24 +// devices and check that the results are as expected.
    1.25 +// Platforms/Drives/Compatibility:
    1.26 +// All.
    1.27 +// Assumptions/Requirement/Pre-requisites:
    1.28 +// Failures and causes:
    1.29 +// Base Port information:
    1.30 +// 
    1.31 +//
    1.32 +
    1.33 +#define __INCLUDE_CAPABILITY_NAMES__
    1.34 +
    1.35 +#define __E32TEST_EXTENSION__
    1.36 +#include <e32test.h>
    1.37 +#include <e32svr.h>
    1.38 +// SYM_BRANCH: Delete old sound driver
    1.39 +// #include <mdasound.h>
    1.40 +#include <d32comm.h>
    1.41 +#include <d32usbc.h>
    1.42 +#include <d32ethernet.h>
    1.43 +
    1.44 +LOCAL_D RTest test(_L("T_SDRIVERS"));
    1.45 +
    1.46 +TCapabilitySet Capabilities;
    1.47 +
    1.48 +LOCAL_C TBool CheckLoaded(TInt aResult)
    1.49 +	{
    1.50 +	switch(aResult)
    1.51 +		{
    1.52 +	case KErrAlreadyExists:
    1.53 +		RDebug::Print(_L("  Already Exists"));
    1.54 +		return ETrue;
    1.55 +
    1.56 +	case KErrNone:
    1.57 +		RDebug::Print(_L("  No Error"));
    1.58 +		return ETrue;
    1.59 +
    1.60 +	case KErrNotFound:
    1.61 +		RDebug::Print(_L("  Not found"));
    1.62 +		return EFalse;
    1.63 +
    1.64 +	default:
    1.65 +		test(EFalse);
    1.66 +		return EFalse;
    1.67 +		}
    1.68 +	}
    1.69 +
    1.70 +
    1.71 +class TDriverCheck;
    1.72 +typedef void (*TDriverCheckTestFunction)(TDriverCheck&);
    1.73 +
    1.74 +class TDriverCheck
    1.75 +	{
    1.76 +public:
    1.77 +	TBool Check(TInt aResult);
    1.78 +	void ShowResult();
    1.79 +public:
    1.80 +	TDriverCheckTestFunction iTestFunction;
    1.81 +	const char* iDeviceName;
    1.82 +	TCapability iCapability;
    1.83 +	TBool iTested;
    1.84 +	TBool iPolicingVerified;
    1.85 +	};
    1.86 +
    1.87 +TBool TDriverCheck::Check(TInt aResult)
    1.88 +	{
    1.89 +	switch(aResult)
    1.90 +		{
    1.91 +	case KErrNotSupported:
    1.92 +		RDebug::Print(_L("  Not Supported"));
    1.93 +		return ETrue;
    1.94 +	case KErrInUse:
    1.95 +		RDebug::Print(_L("  In Use"));
    1.96 +		return ETrue;
    1.97 +	case KErrAccessDenied:
    1.98 +		RDebug::Print(_L("  Access Denied (In Use?)"));
    1.99 +		return ETrue;
   1.100 +	case KErrNone:
   1.101 +		RDebug::Print(_L("  No Error"));
   1.102 +		break;
   1.103 +	case KErrPermissionDenied:
   1.104 +		RDebug::Print(_L("  Permission Denied"));
   1.105 +		break;
   1.106 +	default:
   1.107 +		RDebug::Print(_L("  Error %d"),aResult);
   1.108 +		return ETrue;
   1.109 +		}
   1.110 +
   1.111 +	if(Capabilities.HasCapability(iCapability))
   1.112 +		{
   1.113 +		if(aResult==KErrNone)
   1.114 +			iTested = 1;
   1.115 +		return aResult==KErrNone;
   1.116 +		}
   1.117 +	else if(PlatSec::IsCapabilityEnforced(iCapability))
   1.118 +		{
   1.119 +		if(aResult==KErrPermissionDenied)
   1.120 +			iPolicingVerified = 1;
   1.121 +		return aResult==KErrPermissionDenied;
   1.122 +		}
   1.123 +	else
   1.124 +		{
   1.125 +		return aResult==KErrNone;
   1.126 +		}
   1.127 +	}
   1.128 +
   1.129 +void TDriverCheck::ShowResult()
   1.130 +	{
   1.131 +	TBuf8<32> nameBuf((const TUint8*)iDeviceName);
   1.132 +	TPtr name(nameBuf.Expand());
   1.133 +	if(iTested)
   1.134 +		{
   1.135 +		if(iPolicingVerified)
   1.136 +			test.Printf(_L("*  %S - Verified security checking\n"),&name);
   1.137 +		else
   1.138 +			test.Printf(_L("*  %S - Did NOT verify security checking (Capabilties may be disabled)\n"),&name);
   1.139 +		}
   1.140 +	else
   1.141 +		test.Printf(_L("*  %S - Not tested (Driver may be missing or in use)\n"),&name);
   1.142 +	}
   1.143 +
   1.144 +void TestELOCD(TDriverCheck& aCheck)
   1.145 +	{
   1.146 +	test.Next(_L("ELOCD"));
   1.147 +
   1.148 +	test.Start(_L("Trying RLocalDrive with all local drives"));
   1.149 +	TInt i;
   1.150 +	TInt r;
   1.151 +	for(i=0; i<KMaxLocalDrives; i++)
   1.152 +		{
   1.153 +		RLocalDrive localDrive;
   1.154 +		TInt changedFlag = 0;
   1.155 +		r = localDrive.Connect(i,changedFlag);
   1.156 +		test(aCheck.Check(r));
   1.157 +		localDrive.Close();
   1.158 +		}
   1.159 +	test.End();
   1.160 +	}
   1.161 +
   1.162 +#if defined (__WINS__)
   1.163 +#define COMM_PDD_NAME _L("ECDRV.PDD")
   1.164 +const TInt KMaxCommPdds=0;
   1.165 +#else
   1.166 +#define COMM_PDD_NAME _L("EUART")
   1.167 +const TInt KMaxCommPdds=10;
   1.168 +#endif
   1.169 +
   1.170 +void TestECOMM(TDriverCheck& aCheck)
   1.171 +	{
   1.172 +	test.Next(_L("ECOMM"));
   1.173 +
   1.174 +	test.Start(_L("Load PDDs"));
   1.175 +	TInt i;
   1.176 +	TInt r;
   1.177 +    TBuf<10> pddName=COMM_PDD_NAME;
   1.178 +	for (i=-1; i<KMaxCommPdds; ++i)
   1.179 +		{
   1.180 +		if (i==0)
   1.181 +			pddName.Append(TChar('0'));
   1.182 +		else if (i>0)
   1.183 +			pddName[pddName.Length()-1] = (TText)('0'+i);
   1.184 +		r = User::LoadPhysicalDevice(pddName);
   1.185 +		CheckLoaded(r);
   1.186 +		}
   1.187 +	test.Next(_L("Load LDD"));
   1.188 +	r = User::LoadLogicalDevice(_L("ECOMM.LDD"));
   1.189 +	if(!CheckLoaded(r))
   1.190 +		goto done;
   1.191 +
   1.192 +	test.Next(_L("Open Channels"));
   1.193 +	for(i=0; i<10; i++)
   1.194 +		{
   1.195 +		RBusDevComm commDevice;
   1.196 +		r = commDevice.Open(i);
   1.197 +		test(aCheck.Check(r));
   1.198 +		commDevice.Close();
   1.199 +		}
   1.200 +done:
   1.201 +	test.End();
   1.202 +	}
   1.203 +
   1.204 +void TestEUSBC(TDriverCheck& aCheck)
   1.205 +	{
   1.206 +	test.Next(_L("EUSBC"));
   1.207 +
   1.208 +	test.Start(_L("Load LDD"));
   1.209 +	TInt r = User::LoadLogicalDevice(_L("EUSBC.LDD"));
   1.210 +	if(!CheckLoaded(r))
   1.211 +		goto done;
   1.212 +	test.Next(_L("Open Channel"));
   1.213 +	{
   1.214 +	RDevUsbcClient usbDevice;
   1.215 +	r = usbDevice.Open(0);
   1.216 +	test(aCheck.Check(r));
   1.217 +	usbDevice.Close();
   1.218 +	}
   1.219 +done:
   1.220 +	test.End();
   1.221 +	}
   1.222 +
   1.223 +void TestENET(TDriverCheck& aCheck)
   1.224 +	{
   1.225 +	test.Next(_L("ENET"));
   1.226 +
   1.227 +	test.Start(_L("Load PDD"));
   1.228 +	TInt r = User::LoadPhysicalDevice(_L("ETHERNET.PDD"));
   1.229 +	if(!CheckLoaded(r))
   1.230 +		goto done;
   1.231 +	test.Start(_L("Load LDD"));
   1.232 +	r = User::LoadLogicalDevice(_L("ENET.LDD"));
   1.233 +	if(!CheckLoaded(r))
   1.234 +		goto done;
   1.235 +	test.Next(_L("Open Channel"));
   1.236 +	{
   1.237 +	RBusDevEthernet ethernetDevice;
   1.238 +	r = ethernetDevice.Open(0);
   1.239 +	test(aCheck.Check(r));
   1.240 +	ethernetDevice.Close();
   1.241 +	}
   1.242 +done:
   1.243 +	test.End();
   1.244 +	}
   1.245 +
   1.246 +// SYM_BRANCH: Delete old sound driver
   1.247 +#if 0
   1.248 +void TestESOUND(TDriverCheck& aCheck)
   1.249 +	{
   1.250 +	test.Next(_L("ESOUND"));
   1.251 +
   1.252 +	test.Start(_L("Load PDD"));
   1.253 +	TInt r = User::LoadPhysicalDevice(_L("ESDRV.PDD"));
   1.254 +	if(!CheckLoaded(r))
   1.255 +		goto done;
   1.256 +	test.Next(_L("Load LDD"));
   1.257 +	r = User::LoadLogicalDevice(_L("ESOUND.LDD"));
   1.258 +	if(!CheckLoaded(r))
   1.259 +		goto done;
   1.260 +	test.Next(_L("Open Channel"));
   1.261 +	{
   1.262 +	RMdaDevSound soundDevice;
   1.263 +	r = soundDevice.Open();
   1.264 +	test(aCheck.Check(r));
   1.265 +	soundDevice.Close();
   1.266 +	}
   1.267 +done:
   1.268 +	test.End();
   1.269 +	}
   1.270 +#endif
   1.271 +
   1.272 +TDriverCheck DriverList[] =
   1.273 +	{
   1.274 +		{TestELOCD,"ELOCD",ECapabilityTCB},
   1.275 +		{TestECOMM,"ECOMM",ECapabilityCommDD},
   1.276 +		{TestEUSBC,"EUSBC",ECapabilityCommDD},
   1.277 +		{TestENET,"ENET",ECapabilityCommDD},
   1.278 +// SYM_BRANCH: Delete old sound driver 
   1.279 +//		{TestESOUND,"ESOUND",ECapabilityMultimediaDD},
   1.280 +		{0}
   1.281 +	};
   1.282 +
   1.283 +LOCAL_C TInt DoTests()
   1.284 +	{
   1.285 +	TInt result=0;
   1.286 +	test.Start(_L("Testing all LDDs..."));
   1.287 +	TInt i=0;
   1.288 +	while(DriverList[i].iTestFunction)
   1.289 +		{
   1.290 +		(*DriverList[i].iTestFunction)(DriverList[i]);
   1.291 +		result |= DriverList[i].iTested<<(i*2);
   1.292 +		result |= DriverList[i].iPolicingVerified<<(i*2+1);
   1.293 +		++i;
   1.294 +		}
   1.295 +	test.End();
   1.296 +	return result^0x55555555;
   1.297 +	}
   1.298 +
   1.299 +
   1.300 +enum TTestProcessFunctions
   1.301 +	{
   1.302 +	ETestProcessDoTests,
   1.303 +	};
   1.304 +
   1.305 +
   1.306 +#include "d_sldd.h"
   1.307 +#include "u32std.h"
   1.308 +
   1.309 +RDevice TestDevice;
   1.310 +
   1.311 +TInt TestGetCapsThread(TAny* aDes)
   1.312 +	{
   1.313 +	RThread::Rendezvous(KErrNone);
   1.314 +	TestDevice.GetCaps(*(TDes8*)aDes);
   1.315 +	return KErrNone;
   1.316 +	}
   1.317 +
   1.318 +void TestGetCaps()
   1.319 +	{
   1.320 +	TUint memModelAttributes = UserSvr::HalFunction(EHalGroupKernel, EKernelHalMemModelInfo, NULL, NULL);
   1.321 +	if((memModelAttributes&EMemModelAttrKernProt)==false)
   1.322 +		return; // no kernel protection to test
   1.323 +
   1.324 +	// open test device...
   1.325 +	test.Start(_L("Open test driver"));
   1.326 +	RLddTest ldd;
   1.327 +	_LIT(KTestDeviceName,"D_SLDD");
   1.328 +	TInt r = User::LoadLogicalDevice(KTestDeviceName);
   1.329 +	test(r == KErrNone || r == KErrAlreadyExists);
   1.330 +	r = TestDevice.Open(KTestDeviceName);
   1.331 +	test_KErrNone(r);
   1.332 +	r = ldd.OpenLocal();
   1.333 +	test_KErrNone(r);
   1.334 +
   1.335 +	// get address of some kernel data...
   1.336 +	TUint32* kernelPtr;
   1.337 +	TUint32 kernelData;
   1.338 +	ldd.KernelTestData(kernelPtr,kernelData);
   1.339 +
   1.340 +	// check device GetCaps works...
   1.341 +	test.Next(_L("Check GetCaps"));
   1.342 +	_LIT8(KDummyTestData,"Dummy Test Data");
   1.343 +	TBuf8<256> caps;
   1.344 +	caps.Copy(KDummyTestData);
   1.345 +	test(caps.Compare(KDummyTestData)==0);
   1.346 +	TestDevice.GetCaps(caps);
   1.347 +	test(caps.Compare(KDummyTestData)!=0);
   1.348 +
   1.349 +	// get another thread to try and call device GetCaps to write to kernel data...
   1.350 +	test.Next(_L("Check GetCaps with bad descriptor"));
   1.351 +	TPtr8 badCaps((TUint8*)kernelPtr,sizeof(TUint32));
   1.352 +	RThread thread;
   1.353 +	r = thread.Create(_L("TestGetCapsThread"),TestGetCapsThread,KDefaultStackSize,0x2000,0x2000,(TAny*)&badCaps);
   1.354 +	test_KErrNone(r);
   1.355 +	TRequestStatus ls;
   1.356 +	thread.Logon(ls);
   1.357 +	TRequestStatus rs;
   1.358 +	thread.Rendezvous(rs);
   1.359 +	thread.Resume();
   1.360 +	User::WaitForRequest(rs);
   1.361 +	test_KErrNone(rs.Int());
   1.362 +	User::WaitForRequest(ls);
   1.363 +	test_Equal(EExitPanic,thread.ExitType());
   1.364 +	thread.Close();
   1.365 +
   1.366 +	// check kernel data is unchanged...
   1.367 +	TUint32 kernelData2;
   1.368 +	ldd.KernelTestData(kernelPtr,kernelData2);
   1.369 +	test_Equal(kernelData,kernelData2);
   1.370 +
   1.371 +	// get another thread to try and call device GetCaps with descriptor in kernel memory...
   1.372 +	test.Next(_L("Check GetCaps with bad descriptor 2"));
   1.373 +	r = thread.Create(_L("TestGetCapsThread"),TestGetCapsThread,KDefaultStackSize,0x2000,0x2000,(TAny*)kernelPtr);
   1.374 +	test_KErrNone(r);
   1.375 +	thread.Logon(ls);
   1.376 +	thread.Rendezvous(rs);
   1.377 +	thread.Resume();
   1.378 +	User::WaitForRequest(rs);
   1.379 +	test_KErrNone(rs.Int());
   1.380 +	User::WaitForRequest(ls);
   1.381 +	test_Equal(EExitPanic,thread.ExitType());
   1.382 +	thread.Close();
   1.383 +
   1.384 +	// check kernel data is unchanged...
   1.385 +	ldd.KernelTestData(kernelPtr,kernelData2);
   1.386 +	test_Equal(kernelData,kernelData2);
   1.387 +
   1.388 +	// cleanup...
   1.389 +	ldd.Close();
   1.390 +	TestDevice.Close();
   1.391 +
   1.392 +	test.End();
   1.393 +	}
   1.394 +
   1.395 +
   1.396 +#include "testprocess.h"
   1.397 +
   1.398 +
   1.399 +GLDEF_C TInt E32Main()
   1.400 +    {
   1.401 +	Capabilities = TSecurityInfo(RProcess()).iCaps;
   1.402 +
   1.403 +	test.Title();
   1.404 +
   1.405 +	if(User::CommandLineLength())
   1.406 +		{
   1.407 +		TBuf<128> message;
   1.408 +		__ASSERT_COMPILE(ECapability_Limit<64);
   1.409 +		message.AppendFormat(_L("Tests with capabilities %08x%08x"),((TUint32*)&Capabilities)[1],((TUint32*)&Capabilities)[0]);
   1.410 +		test.Start(message);
   1.411 +		TInt result = DoTests();
   1.412 +		// Don't test.End() so we don't get lots of 'Success's in logs
   1.413 +		return(result);
   1.414 +		}
   1.415 +
   1.416 +	test.Start(_L("Start"));
   1.417 +
   1.418 +	test.Next(_L("Check driver GetCaps() vulnerability"));
   1.419 +	TestGetCaps();
   1.420 +
   1.421 +	TInt i;
   1.422 +	TInt c;
   1.423 +	for(c=0; c<ECapability_Limit; c++)
   1.424 +		{
   1.425 +		RTestProcess p;
   1.426 +		TRequestStatus s;
   1.427 +		TBuf<128> message;
   1.428 +		TCapabilitySet caps;
   1.429 +		caps.SetAllSupported();
   1.430 +		if(!caps.HasCapability((TCapability)c))
   1.431 +			continue;
   1.432 +		caps.RemoveCapability((TCapability)c);
   1.433 +		TBuf8<128> capNameBuf;
   1.434 +		capNameBuf.Copy((const TUint8*)CapabilityNames[c]);
   1.435 +		TPtr capName(capNameBuf.Expand());
   1.436 +		message.AppendFormat(_L("Tests with all capabilities except %S"),&capName);
   1.437 +		test.Next(message);
   1.438 +		p.Create(*(TUint32*)&caps,ETestProcessDoTests);
   1.439 +		p.Logon(s);
   1.440 +		p.Resume();
   1.441 +		User::WaitForRequest(s);
   1.442 +		test(p.ExitType()==EExitKill);
   1.443 +		TInt result=s.Int()^0x55555555;
   1.444 +		i=0;
   1.445 +		while(DriverList[i].iTestFunction)
   1.446 +			{
   1.447 +			if(result & (1<<(i*2)))
   1.448 +				DriverList[i].iTested = ETrue;
   1.449 +			if(result & (1<<(i*2+1)))
   1.450 +				DriverList[i].iPolicingVerified = ETrue;
   1.451 +			++i;
   1.452 +			}
   1.453 +		test((result>>(i*2))==0);
   1.454 +		CLOSE_AND_WAIT(p);
   1.455 +		}
   1.456 +	// Show results requiring manual inspection
   1.457 +	_LIT(KSeperatorText,"----------------------------------------------------------------------------\n"); 
   1.458 +	test.Printf(_L("\n"));
   1.459 +	test.Printf(_L("RESULTS\n")); 
   1.460 +	test.Printf(KSeperatorText);
   1.461 +	i=0;
   1.462 +	while(DriverList[i].iTestFunction)
   1.463 +		{
   1.464 +		DriverList[i].ShowResult();
   1.465 +		++i;
   1.466 +		}
   1.467 +	test.Printf(KSeperatorText);
   1.468 +
   1.469 +	// Wait for a while, or for a key press
   1.470 +	test.Printf(_L("Waiting a short while for key press...\n"));
   1.471 +	TRequestStatus keyStat;
   1.472 +	test.Console()->Read(keyStat);
   1.473 +	RTimer timer;
   1.474 +	test(timer.CreateLocal()==KErrNone);
   1.475 +	TRequestStatus timerStat;
   1.476 +	timer.After(timerStat,20*1000000);
   1.477 +	User::WaitForRequest(timerStat,keyStat);
   1.478 +	TInt key = 0;
   1.479 +	if(keyStat!=KRequestPending)
   1.480 +		key = test.Console()->KeyCode();
   1.481 +	timer.Cancel();
   1.482 +	test.Console()->ReadCancel();
   1.483 +	User::WaitForAnyRequest();
   1.484 +
   1.485 +	test.End();
   1.486 +	return(0);
   1.487 +    }
   1.488 +