Skip to content

Commit 4ce2cf1

Browse files
Deepak Jaindeepakjain111
authored andcommitted
Enhanced security
1 parent e0e95e0 commit 4ce2cf1

File tree

7 files changed

+117
-16
lines changed

7 files changed

+117
-16
lines changed

Unix/base/messages.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ static const MessageField binProtocolNotificationFields[] =
194194
{MFT_POINTER_OPT,offsetof(BinProtocolNotification, user),0,0},
195195
{MFT_POINTER_OPT,offsetof(BinProtocolNotification, password),0,0},
196196
{MFT_POINTER_OPT,offsetof(BinProtocolNotification, authFile),0,0},
197+
{MFT_POINTER_OPT,offsetof(BinProtocolNotification, message),0,0},
197198
{MFT_END_OF_LIST, 0, 0, 0}
198199
};
199200

@@ -233,6 +234,12 @@ static const MessageField pamCheckUserFields[] =
233234
{MFT_END_OF_LIST, 0, 0, 0}
234235
};
235236

237+
static const MessageField pamCheckUserRspFields[] =
238+
{
239+
{MFT_POINTER_OPT,offsetof(PamCheckUserResp, message),0,0},
240+
{MFT_END_OF_LIST, 0, 0, 0}
241+
};
242+
236243
#if defined(CONFIG_ENABLE_PREEXEC)
237244
static const MessageField execPreexecReqFields[] =
238245
{
@@ -298,7 +305,7 @@ static const MessageDeclaration allMessages[] = {
298305
{postSocketFileFields, sizeof(PostSocketFile), MI_TRUE},
299306
{socketMaintenanceFields, sizeof(VerifySocketConn), MI_TRUE},
300307
{pamCheckUserFields, sizeof(PamCheckUserReq), MI_TRUE},
301-
{emptyMessageFields, sizeof(PamCheckUserResp), MI_FALSE}
308+
{pamCheckUserRspFields, sizeof(PamCheckUserResp), MI_FALSE}
302309
#if defined(CONFIG_ENABLE_PREEXEC)
303310
,
304311
{execPreexecReqFields, sizeof(ExecPreexecReq), MI_TRUE},

Unix/base/messages.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ typedef struct _BinProtocolNotification
11911191

11921192
/* if in nonroot mode, keeps track of which socket to send message back*/
11931193
int forwardSock;
1194+
MI_ConstString message;
11941195
}
11951196
BinProtocolNotification;
11961197

@@ -1799,6 +1800,7 @@ typedef struct _PamCheckUserResp
17991800
Message base;
18001801
MI_Uint64 handle;
18011802
MI_Boolean result;
1803+
MI_ConstString message;
18021804
}
18031805
PamCheckUserResp;
18041806

Unix/base/oi_traces.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,10 @@ OI_EVENT("Username exceeds reasonable limit: %d")
437437
void trace_Username_Error(unsigned int bytes);
438438
OI_EVENT("Password exceeds reasonable limit: %d")
439439
void trace_Password_Error(unsigned int bytes);
440+
OI_EVENT("Invalid server credentials")
441+
void trace_InvalidServerCredentials();
442+
OI_EVENT("Attempt to reset secret string")
443+
void trace_AttemptToResetSecretString();
440444

441445

442446

@@ -1861,6 +1865,8 @@ OI_EVENT("AgentMgr_PreExec_ResponseStrand_Close: preexecContext (%p), strand (%p
18611865
void trace_AgentMgr_PreExec_ResponseStrand_Close(void* context, void* strand);
18621866
OI_EVENT("AgentMgr_PreExec_ResponseStrand_Finish: preexecContext (%p), strand (%p)")
18631867
void trace_AgentMgr_PreExec_ResponseStrand_Finish(void* context, void* strand);
1868+
OI_EVENT("Server credentials verified (%p)")
1869+
void trace_ServerCredentialsVerified(void* handle);
18641870

18651871
/******************************** AUTH TRACES ***********************************/
18661872

Unix/base/oiomi.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,18 @@ FILE_EVENT1(20153, trace_Password_Error_Impl, LOG_ERR, PAL_T("Password exceeds r
11331133
#endif
11341134
FILE_EVENT0(20154, trace_Listen_Failed_Impl, LOG_ERR, PAL_T("Listen failed on both IPv4 and IPv6"))
11351135
#if defined(CONFIG_ENABLE_DEBUG)
1136+
#define trace_InvalidServerCredentials() trace_InvalidServerCredentials_Impl(__FILE__, __LINE__)
1137+
#else
1138+
#define trace_InvalidServerCredentials() trace_InvalidServerCredentials_Impl(0, 0)
1139+
#endif
1140+
FILE_EVENT0(20155, trace_InvalidServerCredentials_Impl, LOG_ERR, PAL_T("Invalid Server credentials"))
1141+
#if defined(CONFIG_ENABLE_DEBUG)
1142+
#define trace_AttemptToResetSecretString() trace_AttemptToResetSecretString_Impl(__FILE__, __LINE__)
1143+
#else
1144+
#define trace_AttemptToResetSecretString() trace_AttemptToResetSecretString_Impl(0, 0)
1145+
#endif
1146+
FILE_EVENT0(20156, trace_AttemptToResetSecretString_Impl, LOG_ERR, PAL_T("Attempt to reset Secret String"))
1147+
#if defined(CONFIG_ENABLE_DEBUG)
11361148
#define trace__FindSubRequest_CannotFindKey(a0, a1, a2) trace__FindSubRequest_CannotFindKey_Impl(__FILE__, __LINE__, a0, a1, a2)
11371149
#else
11381150
#define trace__FindSubRequest_CannotFindKey(a0, a1, a2) trace__FindSubRequest_CannotFindKey_Impl(0, 0, a0, a1, a2)
@@ -2411,12 +2423,6 @@ FILE_EVENT2(30212, trace_TrackerHashMapAlreadyExists_Impl, LOG_WARNING, PAL_T("T
24112423
#endif
24122424
FILE_EVENT3(30213, trace_Selector_AddHandler_AlreadyThere_Impl, LOG_WARNING, PAL_T("Selector_AddHandler: selector=%p, handler=%p, name=%T ALREADY REGISTERED"), Selector *, Handler *, const TChar *)
24132425
#if defined(CONFIG_ENABLE_DEBUG)
2414-
#define trace_Selector_RemoveHandler_NotThere(a0, a1, a2) trace_Selector_RemoveHandler_NotThere_Impl(__FILE__, __LINE__, a0, a1, tcs(a2))
2415-
#else
2416-
#define trace_Selector_RemoveHandler_NotThere(a0, a1, a2) trace_Selector_RemoveHandler_NotThere_Impl(0, 0, a0, a1, tcs(a2))
2417-
#endif
2418-
FILE_EVENT3(30214, trace_Selector_RemoveHandler_NotThere_Impl, LOG_WARNING, PAL_T("Selector_RemoveHandler: selector=%p, handler=%p, name=%T NOT REGISTERED"), Selector *, Handler *, const TChar *)
2419-
#if defined(CONFIG_ENABLE_DEBUG)
24202426
#define trace_Stop_OMI() trace_Stop_OMI_Impl(__FILE__, __LINE__)
24212427
#else
24222428
#define trace_Stop_OMI() trace_Stop_OMI_Impl(0, 0)
@@ -2753,6 +2759,12 @@ FILE_EVENT0(40047, trace_Trying_IPv6_Impl, LOG_INFO, PAL_T("Trying to listen on
27532759
#endif
27542760
FILE_EVENT0(40048, trace_TurnOff_IPV6_V6ONLY_Pass_Impl, LOG_INFO, PAL_T("Turn off IPV6_V6ONLY pass."))
27552761
#if defined(CONFIG_ENABLE_DEBUG)
2762+
#define trace_Selector_RemoveHandler_NotThere(a0, a1, a2) trace_Selector_RemoveHandler_NotThere_Impl(__FILE__, __LINE__, a0, a1, tcs(a2))
2763+
#else
2764+
#define trace_Selector_RemoveHandler_NotThere(a0, a1, a2) trace_Selector_RemoveHandler_NotThere_Impl(0, 0, a0, a1, tcs(a2))
2765+
#endif
2766+
FILE_EVENT3(40049, trace_Selector_RemoveHandler_NotThere_Impl, LOG_INFO, PAL_T("Selector_RemoveHandler: selector=%p, handler=%p, name=%T NOT REGISTERED"), Selector *, Handler *, const TChar *)
2767+
#if defined(CONFIG_ENABLE_DEBUG)
27562768
#define trace_FunctionEntered(a0, a1) trace_FunctionEntered_Impl(__FILE__, __LINE__, scs(a0), a1)
27572769
#else
27582770
#define trace_FunctionEntered(a0, a1) trace_FunctionEntered_Impl(0, 0, scs(a0), a1)
@@ -5075,6 +5087,12 @@ FILE_EVENT2(45385, trace_AgentMgr_PreExec_ResponseStrand_Close_Impl, LOG_DEBUG,
50755087
#endif
50765088
FILE_EVENT2(45386, trace_AgentMgr_PreExec_ResponseStrand_Finish_Impl, LOG_DEBUG, PAL_T("AgentMgr_PreExec_ResponseStrand_Finish: preexecContext (%p), strand (%p)"), void*, void*)
50775089
#if defined(CONFIG_ENABLE_DEBUG)
5090+
#define trace_ServerCredentialsVerified(a0) trace_ServerCredentialsVerified_Impl(__FILE__, __LINE__, a0)
5091+
#else
5092+
#define trace_ServerCredentialsVerified(a0) trace_ServerCredentialsVerified_Impl(0, 0, a0)
5093+
#endif
5094+
FILE_EVENT1(45387, trace_ServerCredentialsVerified_Impl, LOG_DEBUG, PAL_T("Server credentials verified (%p)"), void*)
5095+
#if defined(CONFIG_ENABLE_DEBUG)
50785096
#define trace_HTTP_EncryptionFailed() trace_HTTP_EncryptionFailed_Impl(__FILE__, __LINE__)
50795097
#else
50805098
#define trace_HTTP_EncryptionFailed() trace_HTTP_EncryptionFailed_Impl(0, 0)

Unix/http/http.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ typedef void SSL_CTX;
6666
# define SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS 0x0001
6767
#endif
6868

69+
#define INVALID_ID ((uid_t)-1)
70+
6971
int GetTimeStamp(_Pre_writable_size_(TIMESTAMP_SIZE) char buf[TIMESTAMP_SIZE]);
7072

7173
//------------------------------------------------------------------------------
@@ -672,10 +674,13 @@ static Http_CallbackResult _ReadData(
672674
}
673675
}
674676

675-
r = Process_Authorized_Message(handler);
676-
if (MI_RESULT_OK != r)
677+
if (handler->isAuthorised)
677678
{
678-
return PRT_RETURN_FALSE;
679+
r = Process_Authorized_Message(handler);
680+
if (MI_RESULT_OK != r)
681+
{
682+
return PRT_RETURN_FALSE;
683+
}
679684
}
680685

681686
Done:
@@ -1458,6 +1463,8 @@ static MI_Boolean _ListenerCallback(
14581463
h->encryptedTransaction = FALSE;
14591464
h->pSendAuthHeader = NULL;
14601465
h->sendAuthHeaderLen = 0;
1466+
h->authInfo.uid= INVALID_ID;
1467+
h->authInfo.gid= INVALID_ID;
14611468

14621469
h->recvBufferSize = INITIAL_BUFFER_SIZE;
14631470
h->recvBuffer = (char*)PAL_Calloc(1, h->recvBufferSize);

Unix/protocol/protocol.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ MI_Result _AddProtocolSocket_Handler(
216216

217217
static void _ProtocolSocket_Cleanup(ProtocolSocket* handler)
218218
{
219-
ProtocolBase* protocolBase;
219+
ProtocolBase* protocolBase = (ProtocolBase*)handler->base.data;
220220

221221
if(handler->closeOtherScheduled)
222222
return;
@@ -230,6 +230,8 @@ static void _ProtocolSocket_Cleanup(ProtocolSocket* handler)
230230
Batch_Destroy( handler->receivingBatch );
231231
if (handler->engineBatch)
232232
{
233+
// Remove engine's handler from selector list just in case it has not been removed yet.
234+
Selector_RemoveHandler(protocolBase->selector, handler->engineHandler);
233235
Batch_Destroy( handler->engineBatch );
234236
handler->engineBatch = NULL;
235237
}
@@ -258,7 +260,6 @@ static void _ProtocolSocket_Cleanup(ProtocolSocket* handler)
258260
}
259261

260262
// skip for engine communicating with server
261-
protocolBase = (ProtocolBase*)handler->base.data;
262263
if (!protocolBase->forwardRequests || protocolBase->type == PRT_TYPE_LISTENER)
263264
Strand_ScheduleClose( &handler->strand );
264265
}
@@ -673,6 +674,7 @@ static MI_Boolean _SendAuthResponse(
673674
gid_t gid
674675
)
675676
{
677+
ProtocolBase* protocolBase = (ProtocolBase*)h->base.data;
676678
BinProtocolNotification* req;
677679
MI_Boolean retVal = MI_TRUE;
678680

@@ -694,6 +696,16 @@ static MI_Boolean _SendAuthResponse(
694696
}
695697
}
696698

699+
if (protocolBase->expectedSecretString && *protocolBase->expectedSecretString)
700+
{
701+
req->message = Batch_Strdup(req->base.batch, protocolBase->expectedSecretString);
702+
if (!req->message)
703+
{
704+
BinProtocolNotification_Release(req);
705+
return MI_FALSE;
706+
}
707+
}
708+
697709
req->uid = uid;
698710
req->gid = gid;
699711

@@ -1112,9 +1124,17 @@ static MI_Boolean _ProcessEngineAuthMessage(
11121124
/* engine waiting for server's response */
11131125
if (PostSocketFileResponse == sockMsg->type)
11141126
{
1127+
// secret string is mandatory and can be set only during engine start-up
1128+
if( (sockMsg->secretString == NULL) ||
1129+
(*s_secretString && Strncmp(sockMsg->secretString, s_secretString, S_SECRET_STRING_LENGTH) != 0) )
1130+
{
1131+
trace_AttemptToResetSecretString();
1132+
return MI_FALSE;
1133+
}
1134+
11151135
DEBUG_ASSERT(sockMsg->sockFilePath);
11161136
DEBUG_ASSERT(sockMsg->secretString);
1117-
1137+
11181138
Strlcpy(s_socketFile, sockMsg->sockFilePath, PAL_MAX_PATH_SIZE);
11191139
Strlcpy(s_secretString, sockMsg->secretString, S_SECRET_STRING_LENGTH);
11201140
trace_ServerInfoReceived();
@@ -1404,6 +1424,7 @@ static MI_Boolean _SendPamCheckUserResp(
14041424
MI_Boolean result
14051425
)
14061426
{
1427+
ProtocolBase* protocolBase = (ProtocolBase*)h->base.data;
14071428
PamCheckUserResp *req = NULL;
14081429
MI_Boolean retVal = MI_TRUE;
14091430

@@ -1416,6 +1437,16 @@ static MI_Boolean _SendPamCheckUserResp(
14161437
req->handle = handle;
14171438
req->result = result;
14181439

1440+
if (protocolBase->expectedSecretString && *protocolBase->expectedSecretString)
1441+
{
1442+
req->message = Batch_Strdup(req->base.batch, protocolBase->expectedSecretString);
1443+
if (!req->message)
1444+
{
1445+
PamCheckUserResp_Release(req);
1446+
return MI_FALSE;
1447+
}
1448+
}
1449+
14191450
/* send message */
14201451
{
14211452
DEBUG_ASSERT(h->message == NULL);
@@ -1471,6 +1502,19 @@ static MI_Boolean _ProcessPamCheckUserResp(
14711502

14721503
pamMsg = (PamCheckUserResp*) msg;
14731504

1505+
// server authentication check
1506+
if ( (pamMsg->message != NULL) && (*s_secretString) && (Strncmp(pamMsg->message, s_secretString, S_SECRET_STRING_LENGTH) == 0) )
1507+
{
1508+
trace_ServerCredentialsVerified(handler);
1509+
}
1510+
else
1511+
{
1512+
trace_InvalidServerCredentials();
1513+
return MI_FALSE;
1514+
}
1515+
1516+
pamMsg->message = NULL;
1517+
14741518
/* engine waiting server's response */
14751519

14761520
result = authenticateCallback(pamMsg);
@@ -1950,8 +1994,8 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
19501994
return PRT_RETURN_FALSE;
19511995
}
19521996

1953-
DEBUG_ASSERT(s_socketFile != NULL);
1954-
DEBUG_ASSERT(s_secretString != NULL);
1997+
DEBUG_ASSERT(*s_socketFile);
1998+
DEBUG_ASSERT(*s_secretString);
19551999

19562000
/* If system supports connection-based auth, use it for
19572001
implicit auth */
@@ -1986,6 +2030,7 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
19862030
return PRT_RETURN_FALSE;
19872031
}
19882032

2033+
handler->engineHandler = &newSocketAndBase->protocolSocket.base;
19892034
handler->clientAuthState = PRT_AUTH_WAIT_CONNECTION_RESPONSE;
19902035
handler = &newSocketAndBase->protocolSocket;
19912036
newSocketAndBase->internalProtocolBase.forwardRequests = MI_TRUE;
@@ -2009,6 +2054,18 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
20092054
}
20102055
else if (binMsg->type == BinNotificationConnectResponse)
20112056
{
2057+
// server authentication check
2058+
if ( (binMsg->message != NULL) && (*s_secretString) && (Strncmp(binMsg->message, s_secretString, S_SECRET_STRING_LENGTH) == 0) )
2059+
{
2060+
trace_ServerCredentialsVerified(handler);
2061+
}
2062+
else
2063+
{
2064+
trace_InvalidServerCredentials();
2065+
return PRT_RETURN_FALSE;
2066+
}
2067+
binMsg->message = NULL;
2068+
20122069
// forward to client
20132070

20142071
Sock s = binMsg->forwardSock;
@@ -2672,7 +2729,8 @@ MI_Result _ProtocolSocket_New(
26722729
self->closeOtherScheduled = MI_FALSE;
26732730

26742731
self->base.callback = _RequestCallback;
2675-
2732+
self->authInfo.uid = INVALID_ID;
2733+
self->authInfo.gid = INVALID_ID;
26762734
/* Set output parameter */
26772735
*selfOut = self;
26782736
return MI_RESULT_OK;
@@ -3042,6 +3100,8 @@ static MI_Result _ProtocolSocketAndBase_New_Server_Connection(
30423100
protocolSocketAndBase->protocolSocket.refCount = 1; //ref associated with Strand. Released on Strand_Finish
30433101
protocolSocketAndBase->protocolSocket.closeOtherScheduled = MI_FALSE;
30443102
protocolSocketAndBase->protocolSocket.base.callback = NULL;
3103+
protocolSocketAndBase->protocolSocket.authInfo.uid = INVALID_ID;
3104+
protocolSocketAndBase->protocolSocket.authInfo.gid = INVALID_ID;
30453105

30463106
r = _ProtocolBase_Init(&protocolSocketAndBase->internalProtocolBase, selector, NULL, NULL, PRT_TYPE_FROM_SOCKET);
30473107
if( r != MI_RESULT_OK )

Unix/protocol/protocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ typedef struct _ProtocolSocket
118118

119119
/* Whether socket is permanent */
120120
MI_Boolean permanent;
121+
Handler * engineHandler;
121122
}
122123
ProtocolSocket;
123124

0 commit comments

Comments
 (0)