Skip to content

Commit ff62130

Browse files
RamSubbaraoGitHub Enterprise
authored andcommitted
Add more unit tests for mqauth plugin and address memory issue (#684)
1 parent e79b73c commit ff62130

File tree

7 files changed

+258
-97
lines changed

7 files changed

+258
-97
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 9.4.0.0-r3 (2024-07)
44

55
* Fix to diable FIPS mode for `runmqakm` key store generation, when FIPS is not enabled
6+
* Fix APAR IT46430
67

78
## 9.4.0.0 (2024-06)
89

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/testSecret*
2+
/*.log
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
passw0rd
1+
fred:$2y$05$3Fp9

authservice/mqsimpleauth/src/simpleauth.c

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@ limitations under the License.
2323
#include "simpleauth.h"
2424
#include <linux/limits.h>
2525

26+
const char *_mq_app_secret_file = MQ_APP_SECRET_FILE_DEFAULT;
27+
const char *_mq_admin_secret_file = MQ_ADMIN_SECRET_FILE_DEFAULT;
28+
2629
// Check if the user is valid
27-
int simpleauth_authenticate_user(char *user, char *password)
30+
int simpleauth_authenticate_user(const char *const user, const char *const password)
2831
{
2932
int result = -1;
3033

3134
if (simpleauth_valid_user(user))
3235
{
33-
char *pwd = getSecretForUser(user);
36+
char *pwd = get_secret_for_user(user);
3437
if (pwd != NULL)
3538
{
36-
int pwdCheck = strcmp(pwd, password);
37-
if (pwdCheck == 0)
39+
if (strcmp(pwd, password) == 0)
3840
{
3941
log_debugf("Correct password supplied. user=%s", user);
4042
result = SIMPLEAUTH_VALID;
@@ -44,10 +46,12 @@ int simpleauth_authenticate_user(char *user, char *password)
4446
log_debugf("Incorrect password supplied. user=%s", user);
4547
result = SIMPLEAUTH_INVALID_PASSWORD;
4648
}
49+
memset(pwd, 0, strlen(pwd));
4750
free(pwd);
4851
}
4952
else
5053
{
54+
log_debugf("Failed to get secret for user '%s'", user);
5155
result = SIMPLEAUTH_INVALID_PASSWORD;
5256
}
5357
}
@@ -59,81 +63,88 @@ int simpleauth_authenticate_user(char *user, char *password)
5963
return result;
6064
}
6165

62-
bool simpleauth_valid_user(char *user)
66+
bool simpleauth_valid_user(const char *const user)
6367
{
6468
bool valid = false;
65-
if ((strcmp(user, APP_USER_NAME)==0 || strcmp(user, ADMIN_USER_NAME)==0))
69+
if ((strcmp(user, APP_USER_NAME) == 0 || strcmp(user, ADMIN_USER_NAME) == 0))
6670
{
6771
valid = true;
6872
}
6973
return valid;
7074
}
7175

72-
char *getSecretForUser(char *user)
76+
/**
77+
* get_secret_for_user will return a char* containing the credential for the given user
78+
* the credential is read from the filesystem if the relevant file exists and an environment
79+
* variable if not
80+
*
81+
* The caller is responsible for clearing then freeing memory
82+
*/
83+
char *get_secret_for_user(const char *const user)
7384
{
7485
if (0 == strcmp(user, APP_USER_NAME))
7586
{
76-
char *secret = readSecret(MQ_APP_SECRET_FILE);
87+
char *secret = read_secret(_mq_app_secret_file);
7788
if (secret != NULL)
7889
{
7990
return secret;
8091
}
8192
else
8293
{
83-
char* envValue = getenv("MQ_APP_PASSWORD");
84-
if (envValue != NULL)
94+
const char *pwdFromEnv = getenv("MQ_APP_PASSWORD");
95+
if (pwdFromEnv != NULL)
8596
{
8697
log_infof("Environment variable MQ_APP_PASSWORD is deprecated, use secrets to set the passwords");
87-
char* pwdFromEnv = strdup(envValue);
88-
return pwdFromEnv;
89-
}
90-
else
91-
{
92-
return NULL;
9398
}
99+
return strdup(pwdFromEnv);
94100
}
95-
} else if (0 == strcmp(user, ADMIN_USER_NAME))
101+
}
102+
else if (0 == strcmp(user, ADMIN_USER_NAME))
96103
{
97-
char *secret = readSecret(MQ_ADMIN_SECRET_FILE);
104+
char *secret = read_secret(_mq_admin_secret_file);
98105
if (secret != NULL)
99106
{
100107
return secret;
101108
}
102109
else
103110
{
104-
char* envValue = getenv("MQ_ADMIN_PASSWORD");
105-
if (envValue != NULL)
111+
const char *pwdFromEnv = getenv("MQ_ADMIN_PASSWORD");
112+
if (pwdFromEnv != NULL)
106113
{
107114
log_infof("Environment variable MQ_ADMIN_PASSWORD is deprecated, use secrets to set the passwords");
108-
// Get the value of environment variable and store it as a copy to free up the memory
109-
char* pwdFromEnv = strdup(envValue);
110-
return pwdFromEnv;
111115
}
112-
else
113-
{
114-
return NULL;
115-
}
116-
}
116+
return strdup(pwdFromEnv);
117+
}
117118
}
118119
else
119120
{
120121
return NULL;
121122
}
122123
}
123124

124-
char *readSecret(char* secret)
125+
/**
126+
* read_secret will return a char* containing the credential read from the filesystem for the given user
127+
*
128+
* The caller is responsible for clearing then freeing memory
129+
*/
130+
char *read_secret(const char *const secret)
125131
{
126132
FILE *fp = fopen(secret, "r");
127-
const size_t line_size = 1024;
128133
if (fp)
129134
{
135+
const int line_size = MAX_PASSWORD_LENGTH + 1;
130136
char *pwd = malloc(line_size);
131-
char *result = fgets(pwd, line_size, fp);
137+
char *result;
138+
result = fgets(pwd, line_size, fp);
139+
fclose(fp);
132140
if (result == NULL)
141+
{
142+
memset(pwd, 0, line_size);
143+
free(pwd);
133144
return NULL;
134-
135-
fclose(fp);
136-
return pwd;
145+
}
146+
result[strcspn(result, "\r\n")] = 0;
147+
return result;
137148
}
138149
else
139150
{

authservice/mqsimpleauth/src/simpleauth.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ limitations under the License.
2020
#define SIMPLEAUTH_VALID 0
2121
#define SIMPLEAUTH_INVALID_USER 1
2222
#define SIMPLEAUTH_INVALID_PASSWORD 2
23-
#define MQ_APP_SECRET_FILE "/run/secrets/mqAppPassword"
24-
#define MQ_ADMIN_SECRET_FILE "/run/secrets/mqAdminPassword"
23+
#define MQ_APP_SECRET_FILE_DEFAULT "/run/secrets/mqAppPassword"
24+
#define MQ_ADMIN_SECRET_FILE_DEFAULT "/run/secrets/mqAdminPassword"
2525
#define APP_USER_NAME "app"
2626
#define ADMIN_USER_NAME "admin"
27+
#define MAX_PASSWORD_LENGTH 256
28+
29+
extern const char *_mq_app_secret_file;
30+
extern const char *_mq_admin_secret_file;
2731

2832
/**
2933
* Authenticate a user, based on the supplied file name.
@@ -32,27 +36,27 @@ limitations under the License.
3236
* @param password the password of the user
3337
* @return SIMPLEAUTH_VALID, SIMPLEAUTH_INVALID_USER or SIMPLEAUTH_INVALID_PASSWORD
3438
*/
35-
int simpleauth_authenticate_user(char *user, char *password);
39+
int simpleauth_authenticate_user(const char *const user, const char *const password);
3640

3741
/**
3842
* Validate that a user exists in the password file.
3943
*
4044
* @param user the user name to validate
4145
*/
42-
bool simpleauth_valid_user(char *user);
46+
bool simpleauth_valid_user(const char *const user);
4347

4448
/**
4549
* Get the secret of the UserId.
4650
*
4751
* @param user the user name to validate
4852
*/
49-
char *getSecretForUser(char *user);
53+
char *get_secret_for_user(const char *const user);
5054

5155
/**
5256
* Get the secret of the UserId.
5357
*
5458
* @param secret path for the secret file
5559
*/
56-
char *readSecret(char* secret);
60+
char *read_secret(const char *const secret);
5761

5862
#endif

0 commit comments

Comments
 (0)