8000 Fix process_open_sockets type error on darwin by directionless · Pull Request #6546 · osquery/osquery · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process_open_sockets type error on darwin #6546

Merged
merged 2 commits into from
Jul 25, 2021

Conversation

directionless
Copy link
Member

On darwin, soi_so is an unsigned 64bit number. sqlite, however, is
signed. Current behavior is to emit a warning, and drop the column.
It's not clear we have a good fix for the type mismatch, so comment and
drop it in the virtual table instead.

Relates to: #6319

@@ -147,9 +147,14 @@ void genSocketDescriptor(int pid, int descriptor, QueryData& results) {
if (si.psi.soi_family == AF_INET || si.psi.soi_family == AF_INET6) {
Row r;

// sqlite is signed 64bit, but soi_so is unsigned. If it's above
// LLONG_MAX, may as well drop it. That's all we did prior.
if (si.psi.soi_so <= LLONG_MAX) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be cleaner to put this into virtual.cpp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am hesitant to silence this warning without understanding/documenting the scenario. Under what circumstances is this not the case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a good argument to not do something in dynamic_table_row.cpp. But I think we should do something here. As is, this is typical:

osquery> select * from process_open_sockets limit 10;
I0707 15:40:51.245728 278652352 dynamic_table_row.cpp:128] Error casting socket (12188630713064942411) to BIGINT. osquery::ConversionError[1] (Value read is out of the range of representable values by an int. stoll: out of range)
I0707 15:40:51.245841 278652352 dynamic_table_row.cpp:128] Error casting socket (12188630712930999787) to BIGINT. osquery::ConversionError[1] (Value read is out of the range of representable values by an int. stoll: out of range)
+-----+----+--------+--------+----------+---------------+----------------+------------+-------------+------+-------+
| pid | fd | socket | family | protocol | local_address | remote_address | local_port | remote_port | path | state |
+-----+----+--------+--------+----------+---------------+----------------+------------+-------------+------+-------+
| 102 | 7  |        | 2      | 17       | 0.0.0.0       | 0.0.0.0        | 0          | 0           |      |       |
| 102 |    | 16     | 0      | 0        |               |                | 0          | 0           |      |       |
| 276 |    | 3      | 0      | 0        |               |                | 0          | 0           |      |       |
| 286 | 3  |        | 2      | 17       | 0.0.0.0       | 0.0.0.0        | 0          | 0           |      |       |
| 286 |    | 5      | 0      | 0        |               |                | 0          | 0           |      |       |
| 286 |    | 6      | 0      | 0        |               |                | 0          | 0           |      |       |
| 286 |    | 7      | 0      | 0        |               |                | 0          | 0           |      |       |
| 286 |    | 8      | 0      | 0        |               |                | 0          | 0           |      |       |
| 286 |    | 9      | 0      | 0        |               |                | 0          | 0           |      |       |
| 290 |    | 3      | 0      | 0        |               |                | 0          | 0           |      |       |
+-----+----+--------+--------+----------+---------------+----------------+------------+-------------+------+-------+

I can't find much for docs about what soi_so is, other than the bsd headers declare it as uint64. Maybe the family and protocol mean something to someone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on @Smjert's analysis, that soi_so is an address when protocol != 0 I think we should opt to not report anything in this column. I think not reporting anything is better than now and we can leave a note in source code that we are unsure how to find the socket or file descriptor.

@theopolis
Copy link
Member

Ping, what is the next step here? Any thoughts on how we can find the root cause of the large integer values?

@directionless
Copy link
Member Author

Ping, what is the next step here? Any thoughts on how we can find the root cause of the large integer values?

I did some digging before, and I don't have a simple answer. All I found was the header declaration.

Maybe there's a more clever way to handle uint64 -> int64. We could convert the high bit to negation...

@Smjert
Copy link
Member
Smjert commented Oct 31, 2020

Converting a uint64_t to int64_t via casting it's perfectly fine:

int64_t int64socket = static_cast<int64_t>(uint64socket);

Now the question here is more: are there meaningful values of uint64socket that are higher than std::numeric_limits<int64_t>::max()?
If yes, then I would say that the only way is to represent the column as TEXT.

As for family and protocol, those values are defined in the respective socket.h header (https://github.com/apple/darwin-xnu/blob/master/bsd/sys/socket.h).
For instance family 2 is AF_INET, protocol 17 is PF_ROUTE (the same as AF_ROUTE).

EDIT:
Looking in the kernel source code, soi_so seems to be a memory address (offset):
https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/kern/socket_info.c#L70-L72
https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/osfmk/mach/vm_param.h#L311-L315
https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/osfmk/mach/vm_param.h#L334-L344

@@ -147,9 +147,14 @@ void genSocketDescriptor(int pid, int descriptor, QueryData& results) {
if (si.psi.soi_family == AF_INET || si.psi.soi_family == AF_INET6) {
Row r;

// sqlite is signed 64bit, but soi_so is unsigned. If it's above
// LLONG_MAX, may as well drop it. That's all we did prior.
if (si.psi.soi_so <= LLONG_MAX) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on @Smjert's analysis, that soi_so is an address when protocol != 0 I think we should opt to not report anything in this column. I think not reporting anything is better than now and we can leave a note in source code that we are unsure how to find the socket or file descriptor.

On darwin, `soi_so` is an unsigned 64bit number. sqlite, however, is
signed. Current behavior is to emit a warning, and drop the column.
It's not clear we have a good fix for the type mismatch, so comment and
drop it in the virtual table instead.

Relates to: osquery#6319
@directionless directionless force-pushed the seph/macos-socket-types branch from 352eba9 to 1b05bc6 Compare July 25, 2021 16:10
@directionless directionless requested review from a team as code owners July 25, 2021 16:10
@theopolis theopolis merged commit 3fceb76 into osquery:master Jul 25, 2021
@directionless directionless deleted the seph/macos-socket-types branch July 25, 2021 21:26
sharvilshah pushed a commit to sharvilshah/osquery that referenced this pull request Aug 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0