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